proPhet
proPhet

Reputation: 1228

Customised text formatting with php

Using PHP, how would I convert an HTML string say, '+text+' to '<em>text</em>' so that if a user types +text+ in an input, it outputs as text?

Upvotes: 0

Views: 41

Answers (2)

michael.schuett
michael.schuett

Reputation: 4741

if all you want to do is use you can do.

$input = "+text+";

$input = trim($input, '+');

$input = '<em>' . $input . '</em>';

Upvotes: 0

Ali
Ali

Reputation: 3461

You can use str_replace along side explode or regex

$text_string = '+TEST+';
$text = explode("+", $text_string);
$text = $text[1];
$replaced_text = str_replace("+$text+", "<em>$text</em>", $text_string);
echo $replaced_text; // output: <em>TEST</em>

Upvotes: 2

Related Questions