Skip to content
PHP Logo

What is the best practice for manipulating domain values in PHP to remove unwanted prefixes?

When manipulating domain values in PHP to remove unwanted prefixes, the best practice is to use string manipulation functions like `substr` or `str_replace` to extract or replace the prefix. This ensures that the domain value is modified accurately without affecting the rest of the string.

php
// Example code to remove 'www.' prefix from a domain value
$domain = 'www.example.com';
$prefix = 'www.';
$domain = str_replace($prefix, '', $domain);

echo $domain; // Output: example.com