Make text bold with php

Asked

Viewed 2,954 times

0

Good evening, I have a big problem, is the following, I am with a textarea form to send texts to the database, and what I want to do is take a snippet or a word that is contained between certain characters and modify them to a tag... To clarify it is Thus:

Let’s suppose q want to take the text that is within two strokes(-): This is an example -Text- ... And the result I want is, whenever the user puts a word or an excerpt of the text between these two traits(-), I want to exchange them to:: This is a Text of Example.

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

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

So it even works, but like, if I put it twice in a row like this:: /Breno/ /Castro/... He does what I want, but does not add the Space between the two words, IE, I wanted to make the system bold and italic like Whatsapp -Breno- +Castro+, and I realized that here at stackoverflow also has the same system. Please if anyone can help me, I appreciate too many old.

I also warn you that I have tried this:

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

OR

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

And obviously it didn’t work kkkkk It was stupid to have tested like this, but I did it to make sure kkkk

Please guys, help me, I really need

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

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);
  • Dude, very Boom, helped me a lot, but please tell me something, like, you put it in for when you have the text between (-) it replace for (<b>), right up to there beauty, and if I want to put an extra function, like, the (-) is in bold and the (*) stays in Italian, what I have to modify in the code, would have to help me?

  • Edited with the proposed changes.

1

Guys muitooooo Thank you for your attention, and thank you to @Marcelo_uchimura, mt thanks msm guy, helped me very much, I was with mt doubt pq Nn understand well about the function preg_replace, taking advantage, if someone can explain me more about this function or some article well explanatory about it and can indicate me, pfv, I thank mt, and that’s right, thank you for the same help, and I managed 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);
    }

0

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

$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);
}

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

  • Unfortunately it didn’t work, man. /Text/

  • What I want is to select the text between $text and modify it to <b>$text</b> equal to Whatsapp and the stackoverflow site itself

  • So I tested here by sending $text = '/Jorge/ /Silva/' and returned <b>Jorge</b> <b>Silva</b>.

  • Because in mine it was wrong. E wanted instead of using /text/, can only use'* text *'

  • @Brenocastro Like you want it to be?

  • I want the Whatsapp Bold system that when putting a word between (*) -> Text it turns the word into bold

  • 1

    Leandro, mt obg tbm, I’ll use your method to add the space, vllw guy

Show 2 more comments

Browser other questions tagged

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