Louis D.
Louis D.

Reputation: 464

Allow backslash regex

I've a function which replace characters in PHP:

$texte = preg_replace('`\[math\](.+)\[/math\]`isU', '\( $1 \)', $texte);

But I would allow the \ because my strings look like that : \frac{5}{2 \sqrt{5} } x_{k}

EDIT 1 :
For example, the user write the following BBcode : [math] \frac{5}{2 \sqrt{5} } x_{k} [math] it will be translated in HTML by : \( \frac{5}{2 \sqrt{5} } x_{k} \)

EDIT 2 :
This is the function:

$result = preg_replace('`\[math\](.*)\[/math\]`isU', '\( $1 \)', $text);

With $text = "[math] \frac{5}{2 \sqrt{5} } x_{k} [/math]"; It returns \( frac{5}{2 sqrt{5} } x_{k} \) The \ has disappeared.

Upvotes: 0

Views: 124

Answers (2)

Louis D.
Louis D.

Reputation: 464

Finally I found, I have to use

$texte = preg_replace('`\[math\](.*)\[/math\]`isU', '\( $1 \)', $texte);

But I've put this to show $texte : echo'<td>'.BBcode(nl2br(stripslashes(htmlspecialchars($data['post_texte'])))).'

I have just to remove the stripslashes function like this :

echo'<td>'.BBcode(nl2br(htmlspecialchars($data['post_texte']))).'

And it's work fine.

Upvotes: 0

GuyH
GuyH

Reputation: 796

This will do it:

$text = '[math] \frac{5}{2 \sqrt{5} } x_{k} [/math]';
$result = preg_replace('`\[math\](.*)\[/math\]`isU', '( $1 )', $text);
echo "result=$result<br>";

Note I've put the input string in single quotes, so the backslashes don't get interpreted as anything, and you didn't want the backslashes in the replacement string. Hope this is what you want.

Upvotes: 1

Related Questions