doa24uk Posted July 30, 2009 Share Posted July 30, 2009 Hi guys, Here's what I've got so far .... $message = "<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>"; $messageimp = explode (">", $message,1); for($i = 0; $i < count($messageimp); $i++){ echo $messageimp[$i]; } All I want to do is chop $message down so the all I've left with is http://www.mysite.com/?d=9FDS32S Is this possible given that there are double quotes in the original variable?? Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/ Share on other sites More sharing options...
mikesta707 Posted July 30, 2009 Share Posted July 30, 2009 well first of all, that code is rife with syntax errors. when you have double quotes in a string, you need to escape them, IE $string = "quotes\" and quotes\" and quotes\""; Additionally, explode will leave with you 2 chucks <a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank" and http://www.mysite.com/?d=9FDS32S</a I would suggest using a regex function to extract whats between the tags. That would probably be the easiest way to get just the URL Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887148 Share on other sites More sharing options...
TeNDoLLA Posted July 30, 2009 Share Posted July 30, 2009 Or instead escaping use single quotes, that allows you to use double quotes inside string without escaping. Better for html since there is much more double quotes usually in it. And imo its more readable. <?php $message = '<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>'; Edit: only thing if you want variables to echo somewhere among the string or want special characters to appear like new lines for example then use double. Still variables could be done with string concencation with single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887150 Share on other sites More sharing options...
mikesta707 Posted July 30, 2009 Share Posted July 30, 2009 Or instead escaping use single quotes, that allows you to use double quotes inside string without escaping. Better for html since there is much more double quotes usually in it. And imo its more readable. <?php $message = '<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>'; Edit: only thing if you want variables to echo somewhere among the string or want special characters to appear like new lines for example then use double. Still variables could be done with string concencation with single quotes. Ah yes, thats probably the easier way to do it. I never liked doing it that way, for various irrational reasons. Also, here is a good tutorial on regex http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887152 Share on other sites More sharing options...
TeNDoLLA Posted July 30, 2009 Share Posted July 30, 2009 This might do it.. <?php preg_match('/^.*[href=\"](.*)\".*$/', $message, $matches); echo $matches[0]; Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887154 Share on other sites More sharing options...
dreamwest Posted July 30, 2009 Share Posted July 30, 2009 $message = '<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>'; $value=explode('target="_blank">', $message); $value=explode("</a>", $value[1]); $value = trim($value[0]); echo $value; Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887155 Share on other sites More sharing options...
mikesta707 Posted July 30, 2009 Share Posted July 30, 2009 $message = '<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>'; $value=explode('target="_blank">', $message); $value=explode("</a>", $value[1]); $value = trim($value[0]); echo $value; this assumes everyone of his links has the target attribute set _blank, has the target attribute as the last set attribute, that it has double quotes, has no space between it and the ">" character, etc. Regex would be a lot more appliable to different types of links with different attributes Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887158 Share on other sites More sharing options...
dreamwest Posted July 30, 2009 Share Posted July 30, 2009 Of course it does....and this assumes this is a link $message = '<a rel="nofollow" href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a>'; $value=explode("</a>", $message); $value=explode(">", $value[0]); $value = trim($value[1]); echo $value; Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887161 Share on other sites More sharing options...
TeNDoLLA Posted July 30, 2009 Share Posted July 30, 2009 That way it's not very dynamic. What if you use like this <a href="something.php">DOWNLOAD</a> , then it fails. Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887165 Share on other sites More sharing options...
TeNDoLLA Posted July 30, 2009 Share Posted July 30, 2009 Fixed my earlier regexp whcih was not working.. <?php preg_match('/^.*href=\"(.*?)\".*$/', $message, $matches); echo $matches[1]; Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887186 Share on other sites More sharing options...
dreamwest Posted July 30, 2009 Share Posted July 30, 2009 That way it's not very dynamic. What if you use like this <a href="something.php">DOWNLOAD</a> , then it fails. <a href="something.php">DOWNLOAD</a> will become DOWNLOAD See my second post Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887192 Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 problem is i dont think he wants the name of the link, he wants the actual link itself Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887193 Share on other sites More sharing options...
dreamwest Posted July 30, 2009 Share Posted July 30, 2009 Apparently its the same thing: href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a> Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887195 Share on other sites More sharing options...
TeNDoLLA Posted July 30, 2009 Share Posted July 30, 2009 That way it's not very dynamic. What if you use like this <a href="something.php">DOWNLOAD</a> , then it fails. <a href="something.php">DOWNLOAD</a> will become DOWNLOAD See my second post Yes I am aware of that... but I think OP was looking for to get the url what is assigned to href attribute in the link. Edit: Nevermind.. now that I reread the original post. There is same value in link and in href. So yours will do just fine with that. I might have been totally on the wrong tracks.. Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887196 Share on other sites More sharing options...
lonewolf217 Posted July 30, 2009 Share Posted July 30, 2009 Apparently its the same thing: href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a> in this case yes ... in all cases ... maybe not. that is why people are trying to give him a solution that works in ALL cases Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887197 Share on other sites More sharing options...
dreamwest Posted July 30, 2009 Share Posted July 30, 2009 Apparently its the same thing: href="http://www.mysite.com/?d=9FDS32S" target="_blank">http://www.mysite.com/?d=9FDS32S</a> in this case yes ... in all cases ... maybe not. that is why people are trying to give him a solution that works in ALL cases just explode the href=" part to get the url Now say after me $message = 'Regex is the best ! All hail explode'; $value=explode("!", $message); $value = trim($value[1]); echo $value; Quote Link to comment https://forums.phpfreaks.com/topic/168200-explode-not-working/#findComment-887214 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.