Solved

Php preg_match_all for pattern matching

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

20 Experts available now in Live!

Get 1:1 Help Now