Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get substring?

Status
Not open for further replies.

seahorse123

Programmer
Joined
Jul 25, 2005
Messages
39
Location
US
I have file names like:
a11_001.htm
b34_02.htm
c8_003.htm

Now I want to get the substring before character '_', it will show as:
a11
b34
c8

how can I do that?

thanks for any help.
 
I'd use the strpos() and substr() functions:

Code:
<?php
$a = 'a11_001.htm';
$position = strpos($a, '_');

if ($position !== FALSE)
{
	$b = substr ($a, 0, $position);
}
else
{
	$b = $a;
}

print $b;
?>


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top