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.

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

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.