473,657 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2113
In article <7z************ ******@newssvr2 7.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=$matche s[0][0];
$term=$matches[2][0];
$string=str_rep lace($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
886
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 chuck wood?"; $needles = ("wood","chuck","wood chuck"); I want to replace every needle with an anchor tag like this:
3
4317
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' 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...
1
2238
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 output i would expect is as follows:
7
4993
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 and, after submitting via POST to a preg_replace filter, get back '_sometext_' (i.e., the same thing with the literal backslashes stripped)
3
5321
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 --- Sig line1 Sig line2 Sig line3 If I could just get rid of that, it would be pretty good. But I also get this
8
2363
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 or quotation marks around them as well as lines that start with a hyphen. Lastly the lines with ** need the brackets before the **, instead of at the end of the line. Here is what I have so far - it is close, but I keep ending up with
2
1182
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
1897
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 : ) there are so many front pages and but i have a connection include page to use print metatags.. so, what if i put metatags before the <html> tag? is it work for search engines? (especially google and yahoo)
1
1864
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.... protected void Page_Load(object sender, EventArgs e) { HtmlMeta fordesc = new HtmlMeta(); fordesc.Name = "keywords";
0
8397
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7333
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.