Jump to content

Chopping string where HTML tags are also embedded.


mars_rahul

Recommended Posts

i got problem explained as : -

i fetch the data from database which is as :

                        $string = "This is the sample string. <a href=#>click here</a>. Other <b>Rome</b>";

                     

but i need output only the  portion of the string only with <a> tag and content inside of <a> tag and nothing else.

 

output must be as : -

 

                      "<a href=#>click here</a>"

will u please tell me how to solve this problem.

i am new to php so please explain me the process.

 

Regular expressions, look up preg_match().

 

also with a hyperlink at the <a> tag it ends in </a> not [/url] that's just bbcode.

 

You'll want something like

 

<?php
            $pattern = '<a\ href=(.+?)</a>'; //this is the regular expression, probably doesn't work.
            $haystack = $string; //this is your database data.

            $results = preg_match($pattern, $haystack, $matches); //$matches is an array of matches found by preg_match.

           print_r($matches);
?>

Fine here it is, the whole thing done for you :/

 

<?php

$string = "This is the sample string. <a href=#>click here</a>. Other Rome";

$pattern = '^\<a\ href=(.+?)\</a\>^';

$result = preg_match($pattern, $string, $matches);

print_r($matches);

?>

 

And yes that does work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.