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

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
## 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 discussion thread is closed

Replies have been disabled for this discussion.