Jump to content

Adding HTML code to a string in the appropriate place.


ShootingBlanks

Recommended Posts

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)

 

 

 

 

Link to comment
Share on other sites

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 by Drongo_III
Link to comment
Share on other sites

 

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.