Solved

Php preg_match_all for pattern matching

Posted on 2009-05-06
2
246 Views
Last Modified: 2012-05-06
I've used the code below to find matches for [sometextXX] where XX is a numeric. What code do I need to find similar matches where XX is a string which varies, and return the whole string including the brackets and also separately return the XX string in the $matches array?  
<?php

$sourcestring="your source string";

preg_match_all('~\[sometext(\d+)\]~',$sourcestring,$matches);

?>

Open in new window

0
Comment
Question by:ncw
2 Comments
 
LVL 58

Accepted Solution

by:
amit_g earned 500 total points
ID: 24317614
Using \w+ instead of \d+ should give you what you want.

preg_match_all('~\[sometext(\w+)\]~',$sourcestring,$matches);
0
 
LVL 1

Author Closing Comment

by:ncw
ID: 31578587
Thanks very much.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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 count occurrences of each item in an array.
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…

947 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

21 Experts available now in Live!

Get 1:1 Help Now