Jump to content

Simple function to select only a part of a string


mikka23

Recommended Posts

Hi all.  I have encountered a new problem which im not sure how to complete.  Basically I have a string like:

 

<li class="page_item page-item-21"><a href="http://localhost/wordpress/support/" title="Support">Support</a></li>

 

I want a very simple function to only select the anchor from the rest of the string:

 

<a href="http://localhost/wordpress/support/" title="Support">Support</a>

 

Im not sure how to do this at all.  Appreciate any help given.

Link to comment
Share on other sites

Thanks for the reply, I was actually meaning select the whole link from the first string as opposed to taking the actual anchor tag.  The actual inside of the link will change so it will have to be select everything from <a to </a>. im dont know how to do that however.

Link to comment
Share on other sites

Is this more or less what you were after:

 

$text = 'some string <a href="http://www.ask.com" title="none">none</a> exra string';
function returnAnchor($text) {
preg_match('~<a(.*?)</a>~', $text, $matches);
return (is_array($matches)?$matches[0]:'');
}

echo returnAnchor($text);

Link to comment
Share on other sites

Yes, thanks so much.  Thats exactly what I was looking for, now I have the following in my wordpress theme and it works perfectly :)

 

    <?php
function returnAnchor($text) {
preg_match('~<a(.*?)</a>~', $text, $matches);
return (is_array($matches)?$matches[0]:'');
}
?>
<li class="grey"><?php echo returnAnchor(wp_list_pages('title_li=&include=21&echo=0')); ?> </li>

 

Thats helped a lot, 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.