Solved

PHP - find substring within string

Posted on 2010-08-24
7
586 Views
Last Modified: 2012-06-27
Hi,
I have a list of files within a directory,
Is there an easy way to get the number in the string between "AD" and "_" with PHP?

/home/mysite/public_html/uploads/110/AD11026_6_small.jpg
/home/mysite/public_html/uploads/AD12026_15.jpg
/home/mysite/public_html/uploads/120/AD156_4.jpg

Thanks in advance for your comments
0
Comment
Question by:sabecs
7 Comments
 
LVL 4

Expert Comment

by:mhenry20
ID: 33517361
Could use:

$numpart = substr($wholestring,2,strpos($wholestring,'_'));

0
 
LVL 10

Expert Comment

by:honestman31
ID: 33517363
Do u need to get the list in text file ? or u have the list in text format already ?
0
 
LVL 4

Expert Comment

by:mhenry20
ID: 33517372
Hi Sabecs,

I should clarify, in my example $wholestring refers to the string variable holding the directory location.

ie:
$wholestring = '/home/mysite/public_html/uploads/110/AD11026_6_small.jpg'

Thanks,
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 4

Accepted Solution

by:
mhenry20 earned 300 total points
ID: 33517406
Correction:

$numpart = substr($wholestring,strpos($wholestring,'/AD'),strpos($wholestring,'_'));

Forgot to account for all the stuff before AD.

Thanks
0
 
LVL 9

Assisted Solution

by:rg20
rg20 earned 200 total points
ID: 33517409
$ending = string substr ( $string , strrchr($string,"AD") ,strlen ( $string ) )

$number = string substr ( $ending ,0, strrchr($ending ,"_") ) )
0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 33521306
These are all good ways to find the number in a single filename. And you could explode on \n to make an array of filenames and foreach that. However If you want to just find all the numbers then pregmatch is the way to go. I don't have the syntax in front of me but it will creat an array of matches to a regex all with one line of code.
0
 

Author Closing Comment

by:sabecs
ID: 33527400
Thanks for your help...
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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
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 look for a specific file type in a local or remote server directory using PHP.

706 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

18 Experts available now in Live!

Get 1:1 Help Now