Turn bold text with php

0

Good Night, I'm having a big problem, it's the following, I'm having a textarea form to send texts to the database, and what I want to do is to get a snippet or a word that is contained between certain characters and modify them to a tag ... To clarify is this:

Let's suppose I want to get the text that is within two dashes (-): This is a -Text- of Example ... And the result I want is, whenever the user places a word or an excerpt of the text I want to change them to: This is a Text of Example.

I tried with str_replace, but I did not get the desired result.

$text = $_POST['publicacaoText'];
$publicacaoStrip = str_replace("/*", "<b>", $text);
$publicacaoStrip = str_replace("*/", "</b", $publicacaoStrip);

So it goes well, maaas type, if I put it twice in a row like this: Breno Castro / ... He does what I want, But do not add the Espalo between the two words, that is, I wanted to make the system bold and italic like neither WhatsApp -Breno- + Castro + , and I realized that here in the stackoverflow also has the same system. Please if anyone can help me, thank you, you're too old.

Notice also that I have tried this way:

$publicacaoStrip = str_replace("-$text-", "<b>$text</b>", $publicacaoStrip);

OR

$publicacaoStrip = str_replace("-".$text."-", "<b>$text</b>", $publicacaoStrip);

And of course it did not work kkkkk It was stupid to have tested like this, but I made sure kkkkkk

Please help me, I need you so much

If anyone knows how to do this also with JQuery too, but preferred is with PHP please

    
asked by anonymous 29.06.2018 / 00:39

3 answers

3
// Texto em questão.
$publicacaoStrip = 'teste -teste- arr-o-z';

// Expressão regular para negrito.
$reNegrito = '/-([^-]+?)-/';

// Expressão regular para itálico.
$reItalico = '/\*([^\*]+?)\*/';

// Sintaxe de substituição de expressão regular, para negrito.
$replacementNegrito = '<b>$1</b>';

// Sintaxe de substituição de expressão regular, para itálico.
$replacementItalico = '<i>$1</i>';

// Opera a substituição das ocorrências da(s) expressão(ões) regular(es) pelo seu substituto.
$publicacaoStrip = preg_replace(array($reNegrito, $reItalico),
    array($replacementNegrito, $replacementItalico), 
    $publicacaoStrip);

// Ecoa e termina a execução do php.
die($publicacaoStrip);
    
29.06.2018 / 01:15
1

Thank you very much for the attention, and I thank mt @Marcelo_Uchimura, mt thank you msm dear, I was very help, I had mt doubt pq Nn I understand well about the preg_replace function, taking advantage, if anyone can explain me more about this function or some very explanatory article about it and can indicate me, pfv, thank mt, and that's right, thanks for the help even, and I was able to do what I intended

$text = $_POST['publicacaoText'];
    $publicacaoStrip = strip_tags($text);

    // Expressão regular.
    $re = array('/\*([^*]+)\*/', '/-([^-]+?)-/');

    // Sintaxe de substituição de expressão regular.
    $replacement = array('<u>$1</u>', '<b>$1</b>');

    // Opera a substituição das ocorrências da expressão regular pelo seu substituto.
    $publicacaoStrip = preg_replace($re, $replacement, $publicacaoStrip);
    }
    
29.06.2018 / 04:58
0

If you just want the space between them, I think it solves it:

$text = $_POST['publicacaoText'];
// aqui ele quebrará $text toda vez que achar */
$palavras = explode("*/", $text);
// aqui contará as celulas de $palavras
$nP = count($palavras);
// para verificar se houve o post de uma palavra composta ex: /*Breno*/ /*Castro*/, se for apenas uma palavra ele fará essa substituição apenas para uma..
if(isset($palavras[1])){ 
   //caso a célula 1 for inicializada e não for vazia, esse for trocará toda /* por <b> e */ por </b>, sempre irá colocar no mesmo lugar da onde ele pegou a palavra.
   for($i = 0; $i < $nP; $i++){
      $palavras[$i] = str_replace("/*", "<b>", $palavras[$i]);
      $palavras[$i] = str_replace("*/", "</b", $palavras[$i]);       
   }
} else {
  $palavra = str_replace("/*", "<b>", $palavras[0]);
  $palavra = str_replace("*/", "</b", $palavra);
}

So the space you put when you echo the words, this is a very weak solution, for an urgent case like yours, I believe that it is possible to simplify and improve this code that I did.

    
29.06.2018 / 00:53