How to identify a delimiter in a string with php?

Asked

Viewed 144 times

0

In my problem a delimiter is two strings that 'enclose' a part of another string in a given order. For example: '('and'}' are a string delimiter: 'aqui (tem} um delimitador'.

My problem is how to identify a delimiter in a string to replace it with another.

2 answers

3


I believe this function can help you. If you have questions about syntax and other options see http://php.net/manual/en/function.str-replace.php

// Fornece: aqui tem} um delimitador
$bodytag = str_replace( "(", "", "aqui (tem} um delimitador");

In this simple example I just changed the character '(' by ',' I did this to remove the delimiter.

If you want to replace with something put in parameter 2. You can repeat the function to take as many as you need or make an array of unwanted characters.

0

$string = 'ola {cabra} aaa';
$rules = str_replace(array("{","}"), array('[',']'), $string);
print($rules);
//retorna: ola [cabra] aaa
  • The example asked was not ( with } ???

  • I posted here only a draft of the solution of my problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.