I have an html file containing some data, including some urls.
Only on theses urls, I want to replace the _
character by a space (via a php file).
So an url like this:
</p><p><a rel="nofollow" class="external text" href="http://10.20.0.30:1234/index.php/this_is_an_example.html">How_to_sample.</a>
will become
</p><p><a rel="nofollow" class="external text" href="http://10.20.0.30:1234/index.php/this is an example.html">How_to_sample.</a>
This has not to affect the _
that are not on urls.
I think this might be possible with a preg_replace, but i don't know how to proceed for this.
The following code in incorrect as it replace every _
and not just the one in url.
$content2 = preg_replace('/[_]/', ' ', $content);
Thanks.
EDIT:
Thanks for preg_replace_callback
suggestion, this is what I was looking for.
// search pattern
$pattern = '/href="http:\/\/10.20.0.30:1234\/index.php\/(.*?).html">/s';
// the function call
$content2 = preg_replace_callback($pattern, 'callback', $content);
// the callback function
function callback ($m) {
print_r($m);
$url = str_replace("_", " ", $m[1]);
return 'href="http://10.20.0.30:1234/index.php/'.$url.'.html">';
}