Solved

How do I correctly structure > in REGEX

Posted on 2011-02-14
8
435 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

623 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