Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-27-2009, 03:23 PM   PM User | #1
kevinkhan
Regular Coder

 
Join Date: Jun 2009
Posts: 350
Thanks: 75
Thanked 0 Times in 0 Posts
kevinkhan is an unknown quantity at this point
Extracting a href and title out of this string

Hi Guys,

I want to get the href and title out of this html code below..

PHP Code:
<li class="images"><a href="www.something.com/firstpage/" title="title1"><span>7</span></a></li
I set the html code as a string


PHP Code:
$strContent"<li class="images"><a href="www.something.com/firstpage/" title="title1"><span>7</span></a></li>"
and set up this funtion with the regular expression below

PHP Code:

$strListMatches 
'!<li class="images" href="(.*)" title="(.*)"><span>(.*)</span</a></li>!isU';


    function 
getMatches($strMatch,$strContent
  {
        if(
preg_match_all($strMatch,$strContent,$objMatches))
    {
            return 
$objMatches;
        }
        return 
"";
    } 
i tryed this piece of code to print the title but it wont work.

PHP Code:
 print_r($objListMatches[1]); 
Is there something wrong with the regular expression?



iv tryed this regular expression but it wont work

PHP Code:
$strListMatches '!<li class="images" href="(.*)" title="(.*)"><span>(.*)</span></a></li>!isU'
kevinkhan is offline   Reply With Quote
Old 10-27-2009, 03:26 PM   PM User | #2
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
Don't use .* unless you fully understand it. If you want to extract a quoted section, you should use ".*?" or "[^"]*"
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Old 10-27-2009, 03:45 PM   PM User | #3
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
PHP Code:
#<[^>]*href=\"([^\"]*)\"[^>]*>#is 
__________________
Website Design Mansfield
PHP Code:
function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
echo 
I_LOVE(); 
Phil Jackson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:54 AM.


Advertisement
Log in to turn off these ads.