468,758 Members | 2,082 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,758 developers. It's quick & easy.

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 4003
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Edward Patrick | last post: by
2 posts views Thread by Matti Järvinen | last post: by
reply views Thread by Scott May | last post: by
6 posts views Thread by Eric Layman | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.