Jump to content

foreach NOT as...or something


mikefrederick

Recommended Posts

say $x=='http';

 

I want to find every occurrence of a substring within a string, and for each one that is not followed by $x I want to replace the substring with a new string. Need some suggestions on how to pull this one off, wish I could do foreach(substring not as $x)...Thanks guys.

Link to comment
https://forums.phpfreaks.com/topic/91750-foreach-not-asor-something/
Share on other sites

Yeah I don't know I am lost on this one. This is what I was using to replace all href=" with href="(url here), but I only want to do so where the 4 characters after href=" are not http...

 

 

$content=file_get_contents($url,100000);

$content = ereg_replace('href="','href="' . $url,$content);

So you didn't even try it? How about this:

 

<pre>
<?php
$data = <<<DATA
	<a href="http://www.google.com">Google</a>
	<a href="www.phpfreaks.com">PHP Freaks</a>
DATA;
echo htmlspecialchars(
	preg_replace('/(?<=href=")(?!http)[^"]+/', 'linky', $data)
);
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.