?
Solved

Simple Regular Expression Problem.

Posted on 2007-04-02
5
Medium Priority
?
225 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
[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
5 Comments
 
LVL 29

Accepted Solution

by:
TeRReF earned 200 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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

800 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