Get Off My Lawn
Get Off My Lawn

Reputation: 36299

Remove string unless there is a space

I have this regular expression, I would like it to remove all text between $ and $ and replace it with an empty string, UNLESS there is a space anywhere between the two $ signs, and in that case ignore the replacement. With the regexp that I have now, it removes it no matter if there is a space or not.

<?php
$tmp = "<p>
    $random_text$
</p>
<p>
    $random text2$
</p>
<p>
    This is some text
</p>
<p>
    This is some text
</p>";

$tmp = preg_replace("/\\$[^ ].+?\\$/", "", $tmp);

So, in the end I would like to have this as the output. As you may notice, the text between the first paragraph tags are gone, but second one still stands.

<p>

</p>
<p>
    $random text2$
</p>
<p>
    This is some text
</p>
<p>
    This is some text
</p>

Upvotes: 4

Views: 121

Answers (2)

Pawan Thakur
Pawan Thakur

Reputation: 591

<?php $source = 'His $name$ is $Luis$';
echo $result = preg_replace('/\$(.*?)\$/', '<b>$1</b>', $source);
?>

Upvotes: 0

Helio Santos
Helio Santos

Reputation: 6805

/\\$[^ ]+?\\$/

I've just removed the dot

Upvotes: 1

Related Questions