triq
triq

Reputation: 1977

How to search and replace a string in PHP

I need a PHP function that accepts a string, searches for a certain sub string and then replaces that sub string with something else.

Example:

If anyone has any suggestions I'd really appreciate it

Oh yeh, the string is within an element of an array example array[this_string],I need to change the contents of the string within the element of the array.

Thanks!

Upvotes: 1

Views: 304

Answers (2)

Tarek
Tarek

Reputation: 3798

Read about the str_replace function

http://php.net/manual/en/function.str-replace.php

 $raw = 'www.google.com/%hello% Search for: %hello%';
 $str = str_replace("%hello%", "hi", $raw);
 echo $str;

Upvotes: 3

GWW
GWW

Reputation: 44093

$str = str_replace('%hello%','hi',$str);

Upvotes: 3

Related Questions