Page 1 of 1
Changing String
Posted: Sun Sep 01, 2002 2:29 am
by Takuma
I have string like this:-
.CONTENT
it's got "." before the string but I want to replace it to just
CONTENT
how do I do that? Can I just use substr etc or use Regular Expression?
Re: Changing String
Posted: Sun Sep 01, 2002 3:18 am
by gite_ashish
Hi,
substr() -OR- regex will always work but; if its a small task you also go for trim() or any of its varient ltrim(), rtrim().
Code: Select all
<?php
echo trim( '.CONSTANT' ) . '<BR>';
echo trim( '.CONSTANT', '.' ) . '<BR>';
echo substr( '.CONSTANT', 1 );
?>
Regards,
Posted: Mon Sep 02, 2002 1:35 am
by Takuma
Thanks!

Posted: Mon Sep 02, 2002 5:07 am
by theChosen
Code: Select all
<?
$var='.CONTENT';
$var=ereg_replace("^\.","",$var);
echo $var;
?>
Posted: Mon Sep 02, 2002 10:17 am
by Takuma
But wouldn't that be a bit slower since PHP tries to load regular expression module or what ever you call it...

Or is this just for long string

Posted: Mon Sep 02, 2002 10:40 am
by gite_ashish
hi,
yes, definitely the use of regex in place of fixed string makes thing bit slow... since php has to load/process the regex.
regex should be used _only_ when:
1. we have many patters to play with
2. we are not sure about the exact strings; only the patter are known.
for example, country name, we are not sure about the exact string but the expected pattern is any combination of lower case alphabets (a-z), upper case alphabets (A-Z) and space ( ) etc.
regards,
Posted: Mon Sep 02, 2002 11:12 am
by Takuma
Thanks...

I'll remember that.