Solved

PHP - find substring within string

Posted on 2010-08-24
7
594 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

691 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