473,387 Members | 1,890 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,387 software developers and data experts.

preg_replace: 'words' beginning with pattern --> new word

Hi,
I cannot seem to wrap my brain around preg_replace. Though I've read
the help file backwords and forwards. :/ Hoping someone can give me
a solution here.

Problem: Given string 'str' which may contain new lines and will
contain html code, IN this string any "words" that begin with an
underscore I want to replace with a given word. A word here being a
group of chars preceded by a space or null (start of line) and closed
by a space or null or htmltag start<. (Hope that makes sense.)

My current solution/hack:
$str=str_replace( array('_m<br />','_mt<br />','_t<br />'), ' ',
$str);

This is *a* solution, IN THAT I am manually entering into a db the
values _m _mt _t and then, this str_replace will take them and
replace them with a space. Ok, good so far. But I would LIKE to
be able to add new values to the db (ie _x _y _z _example _s7 )
and have THOSE be replaced by a " ".

However this is not the *best solution, obviously. I'm sure it can be
done with preg_replace. I hope!

Other things to note... in the string there are other <br /> tags
which are NOT preceded by a target word.

target words begin with a _ BUT if that is a bad character to choose
it can be anything you want, within reason. *g* In otherwords
starting them with a < would be bad (probably, given my limited info),
and starting target words with an alpha character would be bad because
there are other 'words' which are NOT targetted that begin with alpha.

So... could anyone help me here? If you feel like adding an
explanation as to why your preg_replace works, that would be groovy :)
But beggars cannot be choosers and I would be ecstatic with just the
preg_replace itself!!

Hopefully I haven't left anything out of the string's properties.

Thanks a bazillion,
Sherry
Sherry
--
me(www.CactusBlossom.org) fyi(http://allmyfaqs.com/faq.pl?How_to_post)
Before you criticize someone you should walk a mile in their shoes. That
way when they get angry, you're a mile away and have their shoes.
Jul 17 '05 #1
3 4304
TXSherry wrote:
Hi,
I cannot seem to wrap my brain around preg_replace. Though I've read
the help file backwords and forwards. :/ Hoping someone can give me
a solution here.

Problem: Given string 'str' which may contain new lines and will
contain html code, IN this string any "words" that begin with an
underscore I want to replace with a given word. A word here being a
group of chars preceded by a space or null (start of line) and closed
by a space or null or htmltag start<. (Hope that makes sense.)

My current solution/hack:
$str=str_replace( array('_m<br />','_mt<br />','_t<br />'), ' ',
$str);

This is *a* solution, IN THAT I am manually entering into a db the
values _m _mt _t and then, this str_replace will take them and
replace them with a space. Ok, good so far. But I would LIKE to
be able to add new values to the db (ie _x _y _z _example _s7 )
and have THOSE be replaced by a " ".

However this is not the *best solution, obviously. I'm sure it can be
done with preg_replace. I hope!

Other things to note... in the string there are other <br /> tags
which are NOT preceded by a target word.

target words begin with a _ BUT if that is a bad character to choose
it can be anything you want, within reason. *g* In otherwords
starting them with a < would be bad (probably, given my limited info),
and starting target words with an alpha character would be bad because
there are other 'words' which are NOT targetted that begin with alpha.

So... could anyone help me here? If you feel like adding an
explanation as to why your preg_replace works, that would be groovy :)
But beggars cannot be choosers and I would be ecstatic with just the
preg_replace itself!!

Hopefully I haven't left anything out of the string's properties.

Thanks a bazillion,
Sherry
Sherry
--
me(www.CactusBlossom.org) fyi(http://allmyfaqs.com/faq.pl?How_to_post)
Before you criticize someone you should walk a mile in their shoes. That
way when they get angry, you're a mile away and have their shoes.


....almost got it:
<?php
$text = "abcd_efg anotherword";
$string = preg_replace( "/(_.*?g)/",'<br />',$text);
print $string;
?>

Jul 17 '05 #2
TXSherry wrote:
Hi,
I cannot seem to wrap my brain around preg_replace. Though I've read
the help file backwords and forwards. :/ Hoping someone can give me
a solution here.

Problem: Given string 'str' which may contain new lines and will
contain html code, IN this string any "words" that begin with an
underscore I want to replace with a given word. A word here being a
group of chars preceded by a space or null (start of line) and closed
by a space or null or htmltag start<. (Hope that makes sense.)

My current solution/hack:
$str=str_replace( array('_m<br />','_mt<br />','_t<br />'), ' ',
$str);

This is *a* solution, IN THAT I am manually entering into a db the
values _m _mt _t and then, this str_replace will take them and
replace them with a space. Ok, good so far. But I would LIKE to
be able to add new values to the db (ie _x _y _z _example _s7 )
and have THOSE be replaced by a " ".

However this is not the *best solution, obviously. I'm sure it can be
done with preg_replace. I hope!

Other things to note... in the string there are other <br /> tags
which are NOT preceded by a target word.

target words begin with a _ BUT if that is a bad character to choose
it can be anything you want, within reason. *g* In otherwords
starting them with a < would be bad (probably, given my limited info),
and starting target words with an alpha character would be bad because
there are other 'words' which are NOT targetted that begin with alpha.

So... could anyone help me here? If you feel like adding an
explanation as to why your preg_replace works, that would be groovy :)
But beggars cannot be choosers and I would be ecstatic with just the
preg_replace itself!!

Hopefully I haven't left anything out of the string's properties.

Thanks a bazillion,
Sherry
Sherry
--
me(www.CactusBlossom.org) fyi(http://allmyfaqs.com/faq.pl?How_to_post)
Before you criticize someone you should walk a mile in their shoes. That
way when they get angry, you're a mile away and have their shoes.


done:

<?php
$string_with_underscored_words = 'word word _anotherword _anotherword';
$string = preg_replace( "/(_.*?\b)/" , 'ReplaceWithThis' ,
"$string_with_underscored_words" );
print $string;
?>
Jul 17 '05 #3
## Westcoast Sheri <sh*********@nospamun8nospam.com> wrote:
TXSherry wrote:

....
My current solution/hack:
$str=str_replace( array('_m<br />','_mt<br />','_t<br />'), ' ',
$str);

Thanks a bazillion,
Sherry


done:

<?php
$string_with_underscored_words = 'word word _anotherword _anotherword';
$string = preg_replace( "/(_.*?\b)/" , 'ReplaceWithThis' ,
"$string_with_underscored_words" );
print $string;
?>


Woohoo! :-) Thank you very very much, Sheri!

Brilliant, much better then the str_replace, and I appreciate it
greatly.

Happy 4th !

Sherry
--
me(www.CactusBlossom.org) fyi(http://allmyfaqs.com/faq.pl?How_to_post)
Before you criticize someone you should walk a mile in their shoes. That
way when they get angry, you're a mile away and have their shoes.
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Edward Patrick | last post by:
Hello, I would like to have one servlet serve all requests that do not require any "processing". For example, CSS's, JPG's, etc. I know I can accomlish this with an entry in WEB.XML: ...
2
by: Matti Järvinen | last post by:
Hi I am trying to find a tool for converting XML Docbook to format that is recognized by microsoft word. I have been trying combinations such as (xsltproc, jfor) and (xalan, jfor), but the...
0
by: Linda Cacina | last post by:
Hello all you fine folks, Here is some code I am using to merge data from a single record Access 2K3 table into a NEW word document based on a pre-defined Word merge template doc. All I want to...
0
by: Scott May | last post by:
I am doing a mailmerge from vb.net to word 2003. Everything works fine, but I add a fill-in field to the wrod document and then do a mailmerge the toolbars are missing. Can anyone give me a clue? ...
0
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
2
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
6
by: Eric Layman | last post by:
Hi, I have fields from textareas. With a click of a button, php is able to grab these fields and by using header(), convert the output to Ms Word doc. But the outcome of the word doc...
1
by: fellya | last post by:
Hi, i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python. I've tried to write a simple program that can display a sentence. now my...
3
tpgames
by: tpgames | last post by:
I do not understand why this code does not work? It will show ????? for the word length, but will does not actually access the individual letters within the word list. I enter the vowels as guesses,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.