472,958 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

preg_replace and metatags...

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
5 2072
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alexander Ross | last post by:
I dont think I'll ever understand regular expressions ... I need to do th efollowing and I just don't know where to start: $haystack = "How much wood would a wood chuck chuck if a woodchuck could...
3
by: TXSherry | last post by:
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'...
1
by: yawnmoth | last post by:
say i have the following script: <? $test = "aaaaa"; print '"' . preg_replace('/.*/','x',$test) . '"<br>'; $test = "\n\n\n\n\n"; print '"' . preg_replace('/.*/','x',$test) . '"'; ?> the...
7
by: Margaret MacDonald | last post by:
I've been going mad trying to figure out how to do this--it should be easy! Allow the user to enter '\_sometext\_', i.e., literal backslash, underscore, some text, literal backslash, underscore...
3
by: Charles | last post by:
I'm new to this regular expression stuff. I'd like to use preg_replace to eliminate a known multi-line signature from the body of an E-mail. Say the body text is in $body, and the sig is this ...
8
by: erikcw | last post by:
Hi all, I'm trying to write a regex pattern to use in preg_replace. Basically I want to put around every line (\n) in this variable. However, I need to exclude lines that already have brackets...
2
by: Mike P | last post by:
Is it possible to set a page's metatags programmatically? And if so, how is it done? *** Sent via Developersdex http://www.developersdex.com ***
3
by: Silmaril | last post by:
hi everyone, i have a question about place of metatags. i've started to seo for active webpage of our client(which is not coded by me) ,but website is very bad coded then i'm a lazy programmer...
1
by: Sinan Alkan | last post by:
In a master page --content page system, is it possible to change the "description" and "keywords" meta tags of the master page from content page ? I am trying as follows, from content page.... ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.