Solved

Php preg_match_all for pattern matching

Posted on 2009-05-06
2
249 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

Technology Partners: 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

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…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

740 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