Jump to content


[PHP] preg_replace


No replies to this topic

#1 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 10 February 2008 - 16:31

been messing around with preg_replace for BBcode, e.g.
$str = preg_replace&#40;&#34;/\&#91;b\&#93;&#40;.*?&#41;\&#91;\/b\&#93;/is&#34;, &#34;<strong>$1</strong>&#34;, $str&#41;;

For bold text.

Now i understand pretty much all of it, " (.*?) " is essentially a wildcard, meaning "anything", but is there a way of limiting what it is in there?

for example, when working with size tags, e.g.

&#91;size=x&#93;&#91;/size&#93;


where x should be numeric, and if possible, between 2 pre-defined numbers.

Now i know i could go through each possible outcome, but can this been done via preg_replace (or some other similar function) ?

EDIT: Figuerd out how to limit to numbers:

$str = preg_replace&#40;&#34;/\&#91;size\=\&#34;&#40;&#91;0-9&#93;*?&#41;\&#34;\&#93;&#40;.*?&#41;\&#91;\/size\&#93;/is&#34;, &#34;<span style=&#34;font-size&#58;$1px;&#34;>$2</span>&#34;, $str&#41;;


Still not sure on the limiting the numbers that can be entered though

EDIT2: After finding this very useful forum entry listing all the pattern symbols, i've figured it out. For those interested, here it is:

$str = preg_replace&#40;&#34;/\&#91;size\=\&#34;&#40;&#91;0-5&#93;?&#91;0-9&#93;?&#41;\&#34;\&#93;&#40;.*?&#41;\&#91;\/size\&#93;/is&#34;, &#34;<span style=&#34;font-size&#58;$1px;&#34;>$2</span>&#34;, $str&#41;;


However this does allow people to use size 0, i.e. invisible... but what do i care? you gotta be a tard to use that :dope:

EDIT3: Again for any who are interested, i finally got it completely working (I Hope)

$str = preg_replace&#40;&#34;/\&#91;size\=\&#34;&#40;&#91;1-5&#93;&#91;0-9&#93;{1}|&#91;8-9&#93;{1}&#41;\&#34;\&#93;&#40;.*?&#41;\&#91;\/size\&#93;/is&#34;, &#34;<span style=&#34;font-size&#58;$1px;&#34;>$2</span>&#34;, $str&#41;;


This limits the user to only using sizes between(and including) 8-50

Edited by Bob, 10 February 2008 - 17:33.

Posted Image
F O R T H E N S
Posted Image



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users