Forum Moderators: coopster

Message Too Old, No Replies

remove everything after first $ character

         

ahmed24

3:22 am on Aug 31, 2009 (gmt 0)

10+ Year Member



hi,

i have a variable called $user which sometimes only contains a $ at the end of it or sometimes contains more words and $'s after that. How can i remove everything after and including the first $ sign?

thanks

sastro

3:28 am on Aug 31, 2009 (gmt 0)

10+ Year Member



preg_replace ('/\$(.*)/','',$user);

ahmed24

10:51 am on Aug 31, 2009 (gmt 0)

10+ Year Member



thanks. that works only by removing the $ signs but sometimes i also have text after it so for example it might look like this user1$user2$user3$ but what i want to do is be able to remove everything after the first $ including the $ itself.

at the moment the above makes this: user1$user2$user3$ look like this: user1user2user3

Frank_Rizzo

11:00 am on Aug 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$user = 'user1$user2$user3user4$user5';
$found = strpos($user, '$');
if($found !== false) $user = substr($user, 0, $found);

ALKateb

11:06 am on Aug 31, 2009 (gmt 0)

10+ Year Member



what are u trying to output exactly? cos expression sastro has written will output "user1" and as i understood from u u want to output "user1" of the string "user1$user2$user3$"

ahmed24

11:19 am on Aug 31, 2009 (gmt 0)

10+ Year Member



ALKateb, thats exactly what i want to output. Oh and yes just realised that the expression sastro gave works but only works if usernames dont have "dots" in them so for example if i had user1$user2$ then that would output user1 but if i had:

user.1$user.2$ then that would for some reason output user.1user.2

ALKateb

1:25 pm on Aug 31, 2009 (gmt 0)

10+ Year Member



no ahmed! it will output user.1!
can u post some of ur code where u are making this replacement!

and make sure ure using single quotes ('') not ("")

test it yourself:
<?php

$user = 'user.1$user.2$';
$user = preg_replace ('/\$(.*)/','',$user);
echo $user;

?>

rocknbil

5:13 pm on Aug 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



user1$user2$user3$ but what i want to do is be able to remove everything after the first $ including the $ itself.

$newstring = preg_replace('/^([^\$]+)\$*.*/',$1,$oldstring);

^ string starts with (in THIS context; ^ is a not operator in the class below)

() = store this subpattern in $1

[^\$]+ = subpattern, one or more of any character NOT a $

\$* = followed by zero or more of the the literal $

.* = followed by zero or more of any character

So find anything up to the first $ if it's in the string, store it in $1. If this is followed by a $ and anything else, replace the entire string with $1. This will work whether or not $ is present, that is,

user1
user1$sdfsdf
user1$sdfsd$sdfds$sdfsd

All should store only "user1" in the result.

ahmed24

5:29 pm on Aug 31, 2009 (gmt 0)

10+ Year Member



thanks alot for all your help. i realised i was having trouble because i had $user defined with " instead of ' i didnt know that can make a difference