473,382 Members | 1,750 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,382 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 2093
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.... ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.