Solved

How do I correctly structure > in REGEX

Posted on 2011-02-14
8
434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 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