Solved

Simple Regular Expression Problem.

Posted on 2007-04-02
5
186 Views
Last Modified: 2007-04-02
Hi All,

I Have a simple problem that involves regular expressions.

Here is the string i wish to parse(A HTML document that contains various links with the segment "/user/" in them.

"<HTML><body> blablblabla <a rel="nofollow" href="/user/SomeUser">SomeUser</a>"

I Would like to retrieve all of the values that proceed the /user/ segment of the link, so for example if we have:

<a rel="nofollow" href="/user/Jane">Jane</a>
<a rel="nofollow" href="/user/Bob">Bob</a>

I Would like to produce an array containing both Jane and Bob respectively. I Hope that was clear enough for you.

- Thankyou.
0
Comment
Question by:trent21
  • 3
5 Comments
 
LVL 29

Accepted Solution

by:
TeRReF earned 50 total points
ID: 18837826
How about:

<?php

$s = '<a rel="nofollow" href="/user/Jane">Jane</a>
<a rel="nofollow" href="/user/Bob">Bob</a>';

preg_match_all('/href="\/user\/([^"]+)"/', $s, $m);

$users = $m[1];
print_r($users);

?>
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18837852
You can do this in PHP using ...

$expression = '/href="\/user\/([^"]+)/';
preg_match_all($expression, $string, $matches, PREG_SET_ORDER);

An array will be created named $matches and the second item will have the user names.

Let me know how it works or if you have a question.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18837875
My post is basically a late duplicate of TeRReF's.  I should've refreshed. :)  It is basically the same and Terref's should work with no problem.

bol
0
 

Author Comment

by:trent21
ID: 18837961
Thankyou both! Your solutions work wonders, but since TeRReF was the first, i must give him the credit :)

- Thanks again.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18837994
Your welcome!  I appreciate the thanks.  EE does give you an option to split but I completely agree that Terref should get all the credit for this.  Your note and thanks is great though. :)

Thanks for a fun question.

bol
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

757 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

16 Experts available now in Live!

Get 1:1 Help Now