Page 1 of 1

REPLACE a string

Posted: Thu Jun 26, 2003 3:58 pm
by stc7outlaw
I need to take something out of a variable and I dont know how.
I have

Code: Select all

$id = project_XXXX;
and I want to be able to get pull from that

XXXX

How would I go about taking the project_ out of it?

Posted: Thu Jun 26, 2003 4:08 pm
by daven
if the first part of the variable is always the same (ex: 'project_'), you can do a substr() call to get the remainder.
ex:

Code: Select all

<?php
$id = 'project_XXXX';
$id_sub=substr($id,8);  // $id_sub='XXXX'
?>
On the other hand, if the first part of the $id can be variable ('project_', 'dept_', etc) but contain a common character separating the parts ('_'), use split(), explode(), or preg_split()

Code: Select all

<?php
$id1 = 'project_XXXX';
$id2 = 'dept_YYYY';

$id1_parts=explode("_",$id1);
$id1_sub=$id1_parts[1]; // $id1_sub='XXXX'

$id2_parts=explode("_",$id2);
$id2_sub=$id2_parts[1]; // $id2_sub='YYYY'
?>

Posted: Thu Jun 26, 2003 4:25 pm
by stc7outlaw
thank you