mikefrederick Posted February 18, 2008 Share Posted February 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 18, 2008 Share Posted February 18, 2008 preg_replace('/substring(?!' . preg_quote($x) . ')/', 'replacement', $string); Quote Link to comment Share on other sites More sharing options...
Chris92 Posted February 18, 2008 Share Posted February 18, 2008 Can't you put an if inside your foreach? foreach(substing as $x) { if($x != http) { echo "this isn't http"; } } Sorry if this is irrelivant to what you mean, I'm having a hard time trying to read English Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 18, 2008 Author Share Posted February 18, 2008 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); Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 18, 2008 Author Share Posted February 18, 2008 some help?? Quote Link to comment Share on other sites More sharing options...
effigy Posted February 18, 2008 Share Posted February 18, 2008 preg_replace('/substring(?!' . preg_quote($x) . ')/', 'replacement', $string); Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted February 18, 2008 Author Share Posted February 18, 2008 Yeah I saw, I don't see how that will do what I am saying Quote Link to comment Share on other sites More sharing options...
effigy Posted February 18, 2008 Share Posted February 18, 2008 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.