ShootingBlanks Posted June 21, 2013 Share Posted June 21, 2013 Hello - I have a long string that may or may not contain a full web URL in it. I need to post/echo that string, and if there is a web URL in it, then I need to add: <a href="$url_string" target="_blank"> ...before it, and then after it I have to add: </a> So far, I was using the following code to see if there was a URL in it: $has_url = strpos($my_string, 'http'); if ($has_url) { ...and here is where I'm lost. I get that the logic would be that I need to: 1) Go through the string and pull out all the characters from the "http" until either the string ends (if the URL were the last thing in the string) or until there is a space (since no URLs have spaces, and every URL would be followed by a space, unless it is at the end of the string). 2) Put the text from step 1 above into a variable ($url_string). 3) Find where the "http" starts, and append the following text before it: <a href="$url_string" target="_blank"> 4) Find the end of the URL, and append the following text after it: </a> Unfortunately, I'm far too novice at PHP to figure this out. I've tried some stuff, but none of it seems to work. Hoping someone here can help. Thanks!!! (P.S. - I understand that not all URLs start with "http", but for my purposes the assumption that they do should be fine) Quote Link to comment https://forums.phpfreaks.com/topic/279435-adding-html-code-to-a-string-in-the-appropriate-place/ Share on other sites More sharing options...
Drongo_III Posted June 21, 2013 Share Posted June 21, 2013 (edited) This should do the job. I got this very nifty regex off someone on here. Edited June 21, 2013 by Drongo_III Quote Link to comment https://forums.phpfreaks.com/topic/279435-adding-html-code-to-a-string-in-the-appropriate-place/#findComment-1437310 Share on other sites More sharing options...
Drongo_III Posted June 21, 2013 Share Posted June 21, 2013 (edited) Sry tried to respond to this post but its not saving my code in the code block. Might be because the line length of the regex I am trying to paste in. :/ I know this is frowned upon but going to try and paste this straight into the message window: <?php $STRING = 'This is a https://www.google.com link'; $linkReplace = "<a href=" . "$1" . " target='_blank'>$1</a>"; // Replacement pattern to create a link $text = preg_replace($RegExp, $linkReplace , $STRING); echo $text; Edited June 21, 2013 by Drongo_III Quote Link to comment https://forums.phpfreaks.com/topic/279435-adding-html-code-to-a-string-in-the-appropriate-place/#findComment-1437312 Share on other sites More sharing options...
ShootingBlanks Posted June 21, 2013 Author Share Posted June 21, 2013 Sry tried to respond to this post but its not saving my code in the code block. Might be because the line length of the regex I am trying to paste in. :/ I know this is frowned upon but going to try and paste this straight into the message window: <?php $STRING = 'This is a https://www.google.com link'; $linkReplace = "<a href=" . "$1" . " target='_blank'>$1</a>"; // Replacement pattern to create a link $text = preg_replace($RegExp, $linkReplace , $STRING); echo $text; I don't believe I'm following your post. I tried copying/pasting your exact text as-is (just to see what would happen), and nothing echoed. Where/how do those $1 come into play? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/279435-adding-html-code-to-a-string-in-the-appropriate-place/#findComment-1437313 Share on other sites More sharing options...
Drongo_III Posted June 21, 2013 Share Posted June 21, 2013 (edited) Sorry mate it is just stripping out the regex everytime I try and paste it. I have pasted the code into a temporary repository: http://codetidy.com/5995/ Go there and grab it and let me know how u get on Edited June 21, 2013 by Drongo_III Quote Link to comment https://forums.phpfreaks.com/topic/279435-adding-html-code-to-a-string-in-the-appropriate-place/#findComment-1437314 Share on other sites More sharing options...
ShootingBlanks Posted June 21, 2013 Author Share Posted June 21, 2013 Thank you! :-) Quote Link to comment https://forums.phpfreaks.com/topic/279435-adding-html-code-to-a-string-in-the-appropriate-place/#findComment-1437320 Share on other sites More sharing options...
Christian F. Posted June 22, 2013 Share Posted June 22, 2013 (edited) Just wanted you to know that the above RegExp is the old version, I've since updated it to be even more accurate. This is the latest version of it: http://pastebin.com/7f13wTYe Edited June 22, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/279435-adding-html-code-to-a-string-in-the-appropriate-place/#findComment-1437338 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.