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.

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

echo returnAnchor($text);
?>

 

Using preg_match this is easily possible :)

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.

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

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!

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.