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

preg_replace and metatags...

P: n/a
howdy,

I have some text:

This is {search}Pam Grier{/search}.

What I want is this:

This is <a href="somepage.php?keyword=Pam Grier">Pam Grier</a>.

I am having a hard time figuring out a regex to do this using
preg_replace or ereg_replace.

Could someone offer me a bit of assistance, please?

Thanks in advance,

Paul Brown

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <7z******************@newssvr27.news.prodigy.com >, paul brown wrote:
howdy,

I have some text:

This is {search}Pam Grier{/search}.

What I want is this:

This is <a href="somepage.php?keyword=Pam Grier">Pam Grier</a>.

I am having a hard time figuring out a regex to do this using
preg_replace or ereg_replace.


You could use strpos to find where {search} and {/search} are.
And then with substr filter Pam Grier out.

Looking up wich of the signs in {/search} that have to be escaped, and
then matching {search}(.*?){/search} would put Pam Grier in $1

--
http://home.mysth.be/~timvw
Jul 17 '05 #2

P: n/a
paul brown wrote:
I have some text:

This is {search}Pam Grier{/search}.

What I want is this:

This is <a href="somepage.php?keyword=Pam Grier">Pam Grier</a>. ## Invalid character in URL _______________^_
I am having a hard time figuring out a regex to do this using
preg_replace or ereg_replace.

Could someone offer me a bit of assistance, please?


Try this:

<?php
$txt0 = 'This is {search}Pam Grier{/search}.';

$txt1 = preg_replace('/(.*){search}(.*){\/search}(.*)/i',
## $1 $2 $3
'$1<a href="somepage.php?keyword=$2">$2</a>$3', $txt0);

$txt2 = preg_replace('/(.*){search}(.*){\/search}(.*)/ie',
## evaluate replacement ^
'\'$1<a href="somepage.php?keyword=\' . urlencode(\'$2\') . \'">$2</a>$3\'',
$txt0);

echo "txt0 = $txt0\ntxt1 = $txt1\ntxt2 = $txt2\n\n";
?>

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #3

P: n/a
paul brown wrote:
howdy,

I have some text:

This is {search}Pam Grier{/search}.

What I want is this:

This is <a href="somepage.php?keyword=Pam Grier">Pam Grier</a>.

I am having a hard time figuring out a regex to do this using
preg_replace or ereg_replace.

Could someone offer me a bit of assistance, please?


This should work for you:

<?php
preg_match_all('`(\{search\})(.*)(\{/search\})`iU',$string,$matches);
$search=$matches[0][0];
$term=$matches[2][0];
$string=str_replace($search,'<a href="somepage.php?keyword='.
urlencode($term).'">'.$term.'</a>',$string);
?>

Also, it will take into account the formatting for the search term in
the URL.

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
SEO Competition League: http://seo.koivi.com/
Jul 17 '05 #4

P: n/a
In article <c5************@ID-203069.news.uni-berlin.de>,
Pedro Graca <he****@hotpop.com> wrote:
$txt1 = preg_replace('/(.*){search}(.*){\/search}(.*)/i',
## $1 $2 $3
'$1<a href="somepage.php?keyword=$2">$2</a>$3', $txt0);

I like that comment style! A very good idea with complicated
preg_replaces. Thanks!

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #5

P: n/a
Jan Pieter Kunst wrote:
In article <c5************@ID-203069.news.uni-berlin.de>,
Pedro Graca <he****@hotpop.com> wrote:
$txt1 = preg_replace('/(.*){search}(.*){\/search}(.*)/i',
## $1 $2 $3
'$1<a href="somepage.php?keyword=$2">$2</a>$3', $txt0);

I like that comment style! A very good idea with complicated
preg_replaces. Thanks!


What about extended syntax? I like it even more that that one :)
$txt1 = preg_replace(
'/ # start of regular expression
(.*) # = $1 in replacement string
{search} # literal "search" enclosed in braces
(.*) # = $2
{\/search} # literal "{/search}"
(.*) # = $3
/ix', # end regexp, case insensitive, extended syntax

'$1<a href="somepage.php?keyword=$2">$2</a>$3', $txt0);

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.