Solved

How do I correctly structure > in REGEX

Posted on 2011-02-14
8
430 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
__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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:rlb1
Comment Utility
Thanks Terry!!  Really appreciate it!!  Randy
0
 

Author Comment

by:rlb1
Comment Utility
Thanks Kaufmed!!  Really appreciate you too!  Randy
0
 

Author Closing Comment

by:rlb1
Comment Utility
Thanks to you both
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
= )
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now