473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

simple grep question

Ben
I want to do the following:

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

Any help?

Ben

May 24 '07 #1
4 1295
-----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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sean Berry | last post by:
I am writing a little script to grep through a really long list of files and look for a certain phrase. I wanted to do something like this. li = os.popen("/usr/bin/grep -i %s...
13
by: sf | last post by:
Just started thinking about learning python. Is there any place where I can get some free examples, especially for following kind of problem ( it must be trivial for those using python) I have...
2
by: John E. Jardine | last post by:
Hi, Problem: Executing 's///' has a side effect on grep null string matching. If line 62, the substitution, is executed the last two values returned by grep and printed on lines 68, 69 are...
3
by: David Isaac | last post by:
What's the standard replacement for the obsolete grep module? Thanks, Alan Isaac
4
by: agarwalpiyush | last post by:
Hello, I am going nuts with trying to get the following to work: This is what I intend to do: I have a line in /etc/syslog.conf which I need to delete based on ip-address provided to me in a...
4
by: js | last post by:
Just my curiosity. Can python beats perl at speed of grep-like processing? $ wget http://www.gutenberg.org/files/7999/7999-h.zip $ unzip 7999-h.zip $ cd 7999-h $ cat *.htm bigfile $ du -h...
6
by: beantaxi | last post by:
Hello all, I'm looking for a very simple code analysis tool. I have a large codebase to analyze, and all I really need to do is to find all uses of all methods in a few interfaces. Many tools...
1
by: tapmas | last post by:
Newbie question -- any help very much appreciated: I want to be able to get grep (or whatever else would work) to return not only matching lines, but also the original input string: An example may...
13
by: Anton Slesarev | last post by:
I've read great paper about generators: http://www.dabeaz.com/generators/index.html Author say that it's easy to write analog of common linux tools such as awk,grep etc. He say that performance...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.