Solved

Simple Regular Expression Problem.

Posted on 2007-04-02
5
218 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

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

Suggested Solutions

Title # Comments Views Activity
How do I show metrics with PHP or Javascript 6 46
issue with DB import 1 36
Converting PHP triple des decrypt to c# 9 62
PHP Curl Problem 10 31
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

679 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