Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP - find substring within string

Posted on 2010-08-24
7
Medium Priority
?
601 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
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.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.
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 …
Suggested Courses

609 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