/    Sign up×
Community /Pin to ProfileBookmark

php regular expression

hey guys, I’m new to php regular expressions. I’m working on a forum which will allow the user to type in code (like this site) in between tags and the code will be highlighted. I want to use regular expressions to replace just the text in between the

[code][/code]

tags. I’m having trouble with the pattern, here’s what I’ve got so far:

$inside=eregi(‘([code])+([a-z0-9])+([/code])’,$test,$codetochange);

I’m able to pull all letters and numbers, but I’m not able to pull characters, and if there are tabs, spaces, or returns, then it won’t pick it up either. Is there a way to just get everything in between the

[code][/code]

tags? Thanks!

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 27.2006 — I'm more familiar with the Perl Regexp functions, and would possibly do it as (untested):
[code=php]
preg_replace('/)(.*)/Ui', "<pre>$1</pre>", $codetochange);
[/code]
Copy linkTweet thisAlerts:
@aaronbdavisApr 27.2006 — That will keep the format but it will not hilite the code. You will have to run the variable $1 through another function to do that.

[B]aside:[/B] NogDog, what does the switch /U do in the regex? I have seen you use it before but have never been able to find it in documentation.
Copy linkTweet thisAlerts:
@NogDogApr 27.2006 — That will keep the format but it will not hilite the code. You will have to run the variable $1 through another function to do that.[/quote]
I was assuming you would use your CSS stylesheet to style the PRE block. You'd probably want to assign a class to it for that purpose, e.g.: <pre class='code'>.

[B]aside:[/B] NogDog, what does the switch /U do in the regex? I have seen you use it before but have never been able to find it in documentation.[/QUOTE]
The U makes the search "Ungreedy". Without it, if you had this for your text...
<i>
</i>This [ code]is a test.[ /code] it is [ code]only a test[ /code].

...then without the Ungreedy modifier the search would match once on [b]is a test.[/code] it is [code]only a test[/b] instead of twice on the text within each set of tags.

See http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php for all the available modifiers.
Copy linkTweet thisAlerts:
@felgallApr 27.2006 — Another way to make it ungreedy.

/[code])(.*?)[/code]/
Copy linkTweet thisAlerts:
@bokehApr 27.2006 — Another way to make it ungreedy.

/[code])(.*?)[/code]/[/QUOTE]
It's not quite that simple. People say U the ungreedy modifier but it is not true. What it does in fact is invert greediness. Since expressions are normally greedy by default inverting greediness makes them ungreedy but if the expression contains any ungreedy subexpressions these will be inverted to greedy.
×

Success!

Help @hobeau spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...