Hello everybody,

I need some help with getting a url from href links in a string

example of string:

<?php 
$html = "text <a href="http://www.sitename.com">sitename</a> at <a href="http://www.sitename.com">http://www.sitename.com</a>";
?>

How could I change it to make the string become

<?php 
$html = "text [http://www.sitename.com]sitname at [http://www.sitename.com]";
?>

Notice the 2nd link. I would not want to display that like twice like so [[url]http://www.sitename.com] http://www.sitename.com[/url]. If the anchor text is the same as the url, I would like to only display the url between brackets.

This is what I tried:

<?php 
$urlfind = '/(<a href=")(.*)(">)(.*)(<\/a>)/';
$urlreplace = '[${2}]${4}';
$html = preg_replace($urlfind, $urlreplace, $html);
?>

I appreciate all help.

    Yes, I know the function. But I think it can't help as much as preg_replace in this case. Unless you can prove me wrong ofcourse. 🙂

      I suspect str_replace() will not cut it here.

      Basically, you want to replace:

      <a href="http://www.sitename.com">sitename</a>

      with:

      [http://www.sitename.com]

      Is that correct?

        No, not quite.

        I want to replace this

        <a href="http://www.sitename.com">sitename</a>

        with this

        [http://www.sitename.com]sitename

          Something like this should work:

          $str = '<a href="www.sitename.com">sitename</a>';
          
          $s = preg_match('%<a ([^>]+)?href="(.*)"([^>]+)?>(.*)</a>%', $str, $matches);
          print_r($matches);
          
          $url = $matches[2];
          $sitename = $matches[4];
          
          $new_str = '['.$url.']'.$sitename;
          
            Array
            (
                [0] => <a href="www.sitename.com">sitename</a>
                [1] => 
                [2] => www.sitename.com
                [3] => 
                [4] => sitename
            )

            That is what it said. But how would I make it work with the $html string in my first post. That string can contains text and up to 3 'href' links.

              I appreciate you guys and girls helping me. I know it's not nothing I'm asking here. But I would love to have this working on my site. So thanks to all who have a shot at it.

              Here's what I want one more time.

              I have a string called $html. This string always has alot of text in it and 1, 2 or 3 'href' links. These links are either:
              - linked text: <a href="http://www.url.com">text</a>
              - or linked url's: <a href="http://www.url.com">http://www.url.com</a>

              In case of linked text, I would like to display this:

              [http://www.url.com]text

              In case of linked url, I would like to display this:

              [http://www.url.com]

              I'm not sure if just preg_replace would work though, as it has to check 'href' link up to 3 times and replace all 3 in a correct manner. However, i'm still hoping someone can come up with a wicked solution. Thank you for putting you time in this.

              Yves

                I misunderstood your question the first time I read it. The following may get you a little closer, although my regexp-fu:eek: is not so great. Good luck!

                $str = '
                <a href="www.sitename1.com">sitename1</a><br>
                <a href="www.sitename2.com">sitename2</a><br>
                <a href="www.sitename3.com">sitename3</a><br>';
                
                preg_match_all('%(<a[^>](href=["|\']{1}(.*)["|\']{1})[^>]*>(.*?)</a>)%', $str, $matches);
                
                
                echo 'BEFORE:<br>'.$str.'<br><br>';
                
                for ($a = 0; $a < count($matches[0]); $a++)	{
                	$sitename = $matches[4][$a];
                	$url = $matches[3][$a];
                	$new = '['.$url.']'.$sitename;
                
                $str = preg_replace('%'.$matches[0][$a].'%', $new, $str);
                
                }
                
                echo 'AFTER:<br>'.$str;
                

                  Well, I applied it and checked it out, but it is as if it doesn't do anything.

                  See for yourself by making the $str like this

                  $str = 'I invite you to submit your best quality original articles for massive exposure to the high-traffic <a href="http://smallarticles.com">http://SmallArticles.com</a> expert author community. When you submit your articles to our site, your articles will be picked up by ezine publishers who will reprint your articles with your content and links intact giving you traffic surges to help you increase your sales. To <a href="http://smallarticles.com/">Increase WebSite Traffic Writing Articles</a> by submitting articles at our site, setup a membership account today: <a href="http://smallarticles.com/signup.php">http://SmallArticles.com/Signup.php</a>';

                    Try this:

                    $str = 'I invite you to submit your best quality original articles for massive exposure to the high-traffic <a href="http://smallarticles.com">http://SmallArticles.com</a> expert author community. When you submit your articles to our site, your articles will be picked up by ezine publishers who will reprint your articles with your content and links intact giving you traffic surges to help you increase your sales. To <a href="http://smallarticles.com/">Increase WebSite Traffic Writing Articles</a> by submitting articles at our site, setup a membership account today: <a href="http://smallarticles.com/signup.php">http://SmallArticles.com/Signup.php</a>';
                    
                    
                    preg_match_all('%(<a[^>]href=["|\']{1}([^"|\']+)["|\']{1}>([^<]+)</a>)%', $str, $matches);
                    print_r($matches);
                    
                    echo 'BEFORE:<br>'.$str.'<br><br>';
                    
                    for ($a = 0; $a < count($matches[0]); $a++)	{
                    	$sitename = $matches[3][$a];
                    	$url = $matches[2][$a];
                    	$new = '['.$url.']'.$sitename;
                    
                    $str = preg_replace('%'.$matches[0][$a].'%', $new, $str);
                    
                    }
                    
                    echo 'AFTER:<br>'.$str;
                    

                    Note: My regexp is not too swift so it will need some tweaking... Have a good afternoon.

                      I have to say; I'm impressed. I must of tried it the wrong way before, because I am noticing is does the replacements wonderfully. I will have to check on more examples first. But it is looking good sofar.

                      If it appears to be falwless, I would just have to use preg_replace() to find [iamthesametextasmyneighbour]iamthesametextasmyneighbour and replace them with [iamthesametextasmyneighbour]

                      Thank you for your help, bfuzze_98. You have a good afternoon too! 🙂

                        Write a Reply...