By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,816 Members | 2,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,816 IT Pros & Developers. It's quick & easy.

simple grep question

P: n/a
Ben
I want to do the following:

replace __abc__ with <b>abc</bin a string

Any help?

Ben

May 24 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ben wrote:
I want to do the following:

replace __abc__ with <b>abc</bin a string
Don't use grep. Try this (untested):

preg_replace('/__([a-z0-9]+)__/i', '<b>$1</b>', $string);

You may want to expand the character class.

- --
Edward Z. Yang GnuPG: 0x869C48DA
HTML Purifier <htmlpurifier.org Anti-XSS HTML Filter
[[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGVhZWqTO+fYacSNoRAoRwAJ9sFagIahwNchBKqgY8Rg Pn7qb+fQCfbouO
Yfq3Z6Mii++oowl1GL8ItyE=
=LqGd
-----END PGP SIGNATURE-----
May 24 '07 #2

P: n/a
At Thu, 24 May 2007 15:26:54 -0700, Ben let his monkeys type:
I want to do the following:

replace __abc__ with <b>abc</bin a string

Any help?

Ben
Don't use regex if not necessary.

$fixedstring = str_replace ('__abc__','<b>abc</b>',$yourstring,[$count]);
$count, if supplied, tallies the number of replacements made.

HTH
Sh.
May 25 '07 #3

P: n/a
On 25.05.2007 00:26 Ben wrote:
I want to do the following:

replace __abc__ with <b>abc</bin a string

Any help?

Ben
preg_replace('/__(\w+?)__/', '<b>$1</b>', $string)

or

preg_replace('/\b__(\w+?)__\b/', '<b>$1</b>', $string)

the latter replaces only whole words, i.e. "xxx __blah__ xxx" but not
"zzz__blah___zzz".

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 25 '07 #4

P: n/a
Ben wrote:
replace __abc__ with <b>abc</bin a string
Assuming this is for a simplified text markup system, don't reinvent the
wheel.

Take a look at:

- Markdown
- Textile
- BB Code
- Wiki markup

There are freely available PHP implementations of each of these. Some are
in PEAR -- you'll need to use Google for the others.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 90 days, 22:48.]

Non-Intuitive Surnames
http://tobyinkster.co.uk/blog/2007/0...tive-surnames/
May 25 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.