Changing 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
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Changing String

Post 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?
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Re: Changing String

Post 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,
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Thanks! :D
User avatar
theChosen
Forum Newbie
Posts: 15
Joined: Sun Aug 18, 2002 11:00 am
Location: RO, Europe

Post by theChosen »

Code: Select all

<?
  $var='.CONTENT';
  $var=ereg_replace("^\.","",$var);
  echo $var;
?>
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

But wouldn't that be a bit slower since PHP tries to load regular expression module or what ever you call it... :idea: Or is this just for long string 8O
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post 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,
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Thanks... :D I'll remember that.
Post Reply