ted_chou12 Posted February 25, 2007 Share Posted February 25, 2007 How do you number each foreach() so you can get them easily?? Thanks Ted Link to comment https://forums.phpfreaks.com/topic/40034-can-anyone-tell-me-how-do-you-use-foreach-and/ Share on other sites More sharing options...
ted_chou12 Posted February 25, 2007 Author Share Posted February 25, 2007 nevermind, actually, what i want to do is to detect url in a string and replace it with codes to make it a link, I came up with this, but doesnt work properly, nothing gets changed: //further url detection $strings = explode(" ", $entrycontent); foreach ($strings as $string) { $urlresult = strpos($string, "http://"); if ($urlresult === true) {$urlresults[] = $string;}} foreach ($urlresults as $urlstring) { $entrycontent = str_replace($urlstring, "<a href=\"". $urlstring ."\">". $urlstring ."</a>", $entrycontent);} Can anyone help me please, thanks Link to comment https://forums.phpfreaks.com/topic/40034-can-anyone-tell-me-how-do-you-use-foreach-and/#findComment-193630 Share on other sites More sharing options...
Orio Posted February 25, 2007 Share Posted February 25, 2007 Your if is wrong. Instead of this: if ($urlresult === true) Write this: if ($urlresult !== FALSE) strpos() returns either a number (the position) or false if the string wasn't found. It never returns a boolean true. Orio. Link to comment https://forums.phpfreaks.com/topic/40034-can-anyone-tell-me-how-do-you-use-foreach-and/#findComment-193635 Share on other sites More sharing options...
ted_chou12 Posted February 25, 2007 Author Share Posted February 25, 2007 thanks orio, but now, I am have some problem with strings which are already turned to links, such as <a href="http://test.com">Test</a> and the code still attempts to turn it into another link which is making it a mess, so is it possible to detect if the http:// is the first part of the url: //further url detection $strings = explode(" ", $entrycontent); foreach ($strings as $string) { $urlresult = strpos($string, "http://"); if ($urlresult == 0) {$urlresults[] = $string;}} foreach ($urlresults as $urlstring) { $entrycontent = str_replace($urlstring, "<a href=\"". $urlstring ."\">". $urlstring ."</a>", $entrycontent);} But this doesnt seem to help... because I saw that the manual says that the number indicates the position. Thanks Ted Link to comment https://forums.phpfreaks.com/topic/40034-can-anyone-tell-me-how-do-you-use-foreach-and/#findComment-193643 Share on other sites More sharing options...
Orio Posted February 25, 2007 Share Posted February 25, 2007 Try: if ($urlresult === 0) Because 0==false is true, but 0===flase is false. Orio. Link to comment https://forums.phpfreaks.com/topic/40034-can-anyone-tell-me-how-do-you-use-foreach-and/#findComment-193647 Share on other sites More sharing options...
ted_chou12 Posted February 25, 2007 Author Share Posted February 25, 2007 Thanks! Link to comment https://forums.phpfreaks.com/topic/40034-can-anyone-tell-me-how-do-you-use-foreach-and/#findComment-193667 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.