ereg_replace depreciation is STUPID!

For many years, I would write code like this:


$variable = ereg_replace('hello','goodbye', $varable);

Now, some nut who writes all the server software decided:

“Hey, I’ve got a better idea! Let’s prevent that sim,ple code from working and make people use preg_replace!”, “YEAH!”

I’ve been looking up how to use preg_replace all afternoon, trying to work out how to substitute it for the ereg_replace example above.

All I can find are pages that look like this:

"Oh, well if you want to change a word, you have to use adelimiter and slashes. Here’s a simple to understand example.

Set the demimiter of the punctuation for slashes behind the third loop of the apache module like this:"


preg_replace . "(/+) hello, ("/.*\\.(.*)$/", "\\\\1")+++ and; exit;

Then, simply revert to the 5th dimension, and ask for the continuum transfuctioner, and the wizard that appears behind the third rock will give you an encrypted key to write the second line of code:


(\\b+). "(\\s+2) "(/+) goodbye, "/.*\\.(.*)$/", "\\\\1"()+++ and(\\s+2) ; get coffee;

The, all you have to do is put it together, and you can repace a word. It couldn’t possibly be easier!


preg_replace . "(/+) hello, ("/.*\\.(.*)$/", "\\\\1",)+++ and; exit;
(\\b+). "(\\s+2) "(/+) goodbye, ()+++ and(\\s+2) ; ctrl+alt+del -- x2

Can someone please show me how to redo this, before I seriously destroy everything around me?


[COLOR=#ff0000]$myvariable = ereg_replace('www.','', $myvariable);[/COLOR]

I have tried to work it out for 3 hours, but these articles are giving me severe head trauma:

Example 1
Example 2
Example 3

For simple string replacements you can just use [fphp]str_replace[/fphp].


$variable = 'hello, world';
$variable = str_replace('hello', 'goodbye', $variable);
echo $variable; // goodbye, world

$myvariable = 'www.example.com';
$myvariable = str_replace('www.', '', $myvariable);
echo $myvariable; // example.com

Only when you need regular expressions do you need preg_replace :slight_smile:

Oh, so…

…I spent three hours, teetering on the edge of “happy land” (across the road from the loony asylum), for nothing?

It’s that simple?

Yes, it really is that simple. Only regular expressions, so when you don’t know what string you need to match, but you do know a pattern like “it should be a valid e-mail address”, need preg_replace. For all strings you know the value of you can just use str_replace :slight_smile:

Or, if you want multiple replaces and don’t want a later one to match the result of an earlier one there is strtr.


$variable = strtr('Hello. Goodbye.', array('Hello'=>'Goodbye', 'Goodbye'=>'Hello'));
echo $variable; //Goodbye. Hello.

(using two str_replace after one another you would have gotten Hello. Hello.)

For reference (even if you don’t need it), in preg_replace it would have been


$variable = 'www.example.com';
$variable = preg_replace('~www\\.~', '', $variable);
echo $variable;

where

[table]
[tr][td]~[/td][td]start of the regular expression[/td][/tr]
[tr][td]www[/td][td]match the text “www” literally[/td][/tr]
[tr][td]\.[/td][td]match a dot literally – the dot in regular expression has a special meaning, namely “any character” – i.e., S.all.oX.X matches ScallioXTX (but also SballooXRX, SyalluoXPX, etc)[/td][/tr]
[tr][td]~[/td][td]end of the regular expression[/td][/tr]
[/table]

Why does the Internet let this happen?

lol, j/k.

I was happy with your first response :slight_smile:

I just come back from testing str_replace, and it worked fine.

I extend to you great appreciation!

Thanks :slight_smile: