?
Solved

PHP - find substring within string

Posted on 2010-08-24
7
Medium Priority
?
599 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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!

 
LVL 4

Accepted Solution

by:
mhenry20 earned 1200 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 800 total points
ID: 33517409
$ending = string substr ( $string , strrchr($string,"AD") ,strlen ( $string ) )

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

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

741 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