Solved

Simple Regular Expression Problem.

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
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.

696 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