Solved

Php preg_match_all for pattern matching

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 dynamically set the form action using jQuery.

770 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