Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Simple Regular Expression Problem.

Posted on 2007-04-02
5
Medium Priority
?
230 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

604 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