Solved

How do I correctly structure > in REGEX

Posted on 2011-02-14
8
432 Views
Last Modified: 2012-05-11
Experts:
I have a problem with structuring a REGEX statement.  How do I fix this?  I have attempted \s\>\s
 >
\s>\s
/&gt/ which I do not think needs to be ecaped.


<div id="product_header">
        <a href="/categories.asp?cat_id=100">Adapters, Connectors</a> &gt; <a href="/categories.asp?cat_id=881003">PC Adapters and Connectors</a> &gt; <a href="/categories.asp?cat_id=138">Din & Mini-Din Connectors</a>
    </div>



Currently, the only result I get is:
100
Adapters, Connectors



The result should be:
100
Adapters, Connectors
881003
PC Adapters and Connectors
138
Din & Mini-Din Connectors



I am assuming that the problem is in the  &gt; .  

Thanks for your help!!
preg_match_all('%<div id="product_header">
        <a href="\/categories\.asp\?cat_id=(.*?)">(.*?)<\/a>\s\&gt;\s<a href="\/categories\.asp\?cat_id=(.*?)">(.*?)<\/a> &gt; <a href="\/categories\.asp\?cat_id=(.*?)">(.*?)<\/a>
    <\/div>%',$data,$matches2,PREG_SET_ORDER);

Open in new window

0
Comment
Question by:rlb1
  • 3
  • 3
  • 2
8 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 34891297
This works... I'm not actually sure where the problem was, but I gave it a general tidy up and it started working:

preg_match_all('%<div id="product_header">\s*<a href="/categories\.asp\?cat_id=([^"]*?)">(.*?)</a>\s*&gt;\s*<a href="/categories\.asp\?cat_id=([^"]*?)">([^<]*?)</a> &gt; <a href="/categories\.asp\?cat_id=([^"]*?)">(.*?)</a>\s*<\/div>%',$data,$matches2,PREG_SET_ORDER);
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34891323
Actually, your code seems to work without changes, now that I retry it (but I still recommend giving it a tidy up as provided). I suspect the issue may have been with how you were looking at the results. Try doing a:

print_r($matches2);

to see ALL the results if you haven't done that already.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34891374
For the record, you don't need to escape / characters or & characters, provided you aren't using them as pattern terminators (you're using % for that). You only need to escape these, from memory:
[]().?*+{}\^$
and your pattern terminator, if you want to match it.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34891444
__NO POINTS__

Agreed. You're code worked without incident for me as well:


<?php

$data = "<div id=\"product_header\">
        <a href=\"/categories.asp?cat_id=100\">Adapters, Connectors</a> &gt; <a href=\"/categories.asp?cat_id=881003\">PC Adapters and Connectors</a> &gt; <a href=\"/categories.asp?cat_id=138\">Din & Mini-Din Connectors</a>
    </div>";

preg_match_all('%<div id="product_header">
        <a href="\/categories\.asp\?cat_id=(.*?)">(.*?)<\/a>\s\&gt;\s<a href="\/categories\.asp\?cat_id=(.*?)">(.*?)<\/a> &gt; <a href="\/categories\.asp\?cat_id=(.*?)">(.*?)<\/a>
    <\/div>%',$data,$matches2,PREG_SET_ORDER);

var_dump($matches2);

?>

Open in new window

untitled.png
0
 

Author Comment

by:rlb1
ID: 34891535
Thanks Terry!!  Really appreciate it!!  Randy
0
 

Author Comment

by:rlb1
ID: 34891544
Thanks Kaufmed!!  Really appreciate you too!  Randy
0
 

Author Closing Comment

by:rlb1
ID: 34891582
Thanks to you both
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34891674
= )
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question