REPLACE a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
stc7outlaw
Forum Newbie
Posts: 21
Joined: Mon Jun 09, 2003 9:36 pm

REPLACE a string

Post 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?
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post 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'
?>
User avatar
stc7outlaw
Forum Newbie
Posts: 21
Joined: Mon Jun 09, 2003 9:36 pm

Post by stc7outlaw »

thank you
Post Reply