String handling with str_replace

Asked

Viewed 149 times

2

I’m trying to treat a string with the function str_replace but I wanted to insert a <span> before the word treated, as in the example below:

$entrada = array("//");
$tratamento   = array("<span class='comentario'>//</span>");
$saida = str_replace($entrada, $tratamento, $mensagem);

My doubt:

It is possible to do this with the function str_replace, or only with regular expressions, if it is only with ER’s I would like you to give me examples.

I wish after I did that I would return something like:

<span class='comentario'>//</span>

Valeu Galera!

  • Do you want to turn a predetermined word into span, or do you want everything between // and end of line between span? Whether the text will vary outside of code control, only ER itself, or a combination of initial string location and final string, and substitution, which can be much faster than regex/ER.

  • @Bacco I want everything between // and end of the line between in span, how can I do this?

1 answer

2


Example with ER’s:

$entrada = "/Olá¡ hoje é domingo/";
$saida = preg_replace('/(\/)(.*)(\/)/',"<span class='comentario'>$2<span>",$entrada);

If you want to "pick up the contents from // even a new line can use: '/\/\/(.*)/' - Example

Example with explode:

$entrada = "//Olá, hoje é domingo";
$entradaArr = explode('//', $entrada);
$tratamento   = array("<span class='comentario'>", "</span>");
$saida = $tratamento[0].$entradaArr[1].$tratamento[1];
  • From what I understand, it would be more the case to find from // until the end of the line, as /abc123 -> <span>//abc123</span>

  • 1

    @Bacco, Aha, ok. I added this option. I’m going for example too.

  • @Bacco, thanks for the help, it worked here [off topic] You could show me something to understand the logic of regular expressions?

  • 2

    @Odair, look at this link, is an excellent site for Regex, with the explanation below each piece.

  • Pow, thanks @Sergio I will give a studied yes. Thank you so much for the help! E, man! what a mass site this ahaha

  • @Bacco, it worked right, the only problem is that it doesn’t only put span in the first line, but in my entire text, can you change it? This is my ER: $text = preg_replace("/(//)(. *)/is",'<span class="comment">//$2<span>',$text);

  • @Odair, I think you want to write @Sergio, not Bacco :) Can you give me a concrete example with your php code?

  • Sorry @Sergio that’s right rs so I made a bbcode function Function Bbcode($text) {$text = preg_replace("/(//)(. *)/",'<span class="comment">//$2<span>',$text);}

  • @Odair, and can put one/two examples of a string that $text may be?

  • With pleasure rs, @Sergio, here’s the $text I’m trying to deal with with regular expressions :D http://jsbin.com/xepib/1/edit

  • @Sergio, sorry to ask again, but another problem came up, my bbcode puts <br> tag on the line break and that example you sent me it picks up after n how can I adapt the script so that it applies span only before the <br tag>?

  • @Odair, what you can do is add one <br /> after span when do replace.

Show 8 more comments

Browser other questions tagged

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