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. Link to comment https://forums.phpfreaks.com/topic/91750-foreach-not-asor-something/ 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); Link to comment https://forums.phpfreaks.com/topic/91750-foreach-not-asor-something/#findComment-469921 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 Link to comment https://forums.phpfreaks.com/topic/91750-foreach-not-asor-something/#findComment-469923 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); Link to comment https://forums.phpfreaks.com/topic/91750-foreach-not-asor-something/#findComment-469967 Share on other sites More sharing options...
mikefrederick Posted February 18, 2008 Author Share Posted February 18, 2008 some help?? Link to comment https://forums.phpfreaks.com/topic/91750-foreach-not-asor-something/#findComment-469975 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); Link to comment https://forums.phpfreaks.com/topic/91750-foreach-not-asor-something/#findComment-469977 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 Link to comment https://forums.phpfreaks.com/topic/91750-foreach-not-asor-something/#findComment-469980 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> Link to comment https://forums.phpfreaks.com/topic/91750-foreach-not-asor-something/#findComment-469986 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.