Solved

PHP - find substring within string

Posted on 2010-08-24
7
587 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
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.

 
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

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

930 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

13 Experts available now in Live!

Get 1:1 Help Now