Hi
I want to replace every 4th and higher occurence of "." with / in a string.
For eg:
if i have some string like http://www.google.com.news , http://www.google.com.sports, then I would like to have something like http://www.google.com/news and http://www.google.com/sports
How do i do it with regular expression ??
php regular expression
Moderator: General Moderators
Re: php regular expression
Fourth and higher? I only see three.
I don't see why people always think regular expressions fix everything. They're powerful, yeah, but they don't fix everything and much of the time they're a bad solution.
Hidden "feature": more than one consecutive period in the domain name gets reduced to only one.
I don't see why people always think regular expressions fix everything. They're powerful, yeah, but they don't fix everything and much of the time they're a bad solution.
Code: Select all
$string = "http://www.google.com.news";
$first = strtok($string, ".") and $string2 = $first;
$second = strtok(".") and $string2 .= "." . $second;
$third = strtok(".") and $string2 .= "." . $third;
// $fourth = strtok(".") and $string2 .= "." . $fourth;
$rest = strtok("") and $string2 .= "/" . str_replace(".", "/", $rest);
echo $string2;