Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple: How to replace "all between" with php? [duplicate]

$string = "<tag>i dont know what is here</tag>" $string = str_replace("???", "<tag></tag>", $string); echo $string; // <tag></tag> 

So what code am i looking for?

like image 902
faq Avatar asked Jul 29 '11 16:07

faq


People also ask

How can I replace multiple words in a string in PHP?

Approach 1: Using the str_replace() and str_split() functions in PHP. The str_replace() function is used to replace multiple characters in a string and it takes in three parameters. The first parameter is the array of characters to replace.

How can I replace part of a string in PHP?

The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.

How can I replace one word with another in PHP?

Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.

How can I replace all characters in a string using jQuery?

The replaceAll() method is an inbuilt method in jQuery which is used to replace selected elements with new HTML elements. Parameters: This method accepts two parameter as mentioned above and described below: content: It is the required parameter which is used to specify the content to insert.


1 Answers

A generic function:

function replace_between($str, $needle_start, $needle_end, $replacement) {     $pos = strpos($str, $needle_start);     $start = $pos === false ? 0 : $pos + strlen($needle_start);      $pos = strpos($str, $needle_end, $start);     $end = $pos === false ? strlen($str) : $pos;      return substr_replace($str, $replacement, $start, $end - $start); } 

DEMO

like image 124
Felix Kling Avatar answered Sep 23 '22 14:09

Felix Kling