Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I correctly structure > in REGEX

Posted on 2011-02-14
8
Medium Priority
?
436 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 2000 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
Industry Leaders: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.

704 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