The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 17, 2019, 06:01:25 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: exploding with multiple delimeters?  (Read 3625 times)
deregular
Expert
****
Offline Offline

Posts: 172


View Profile
« on: May 21, 2009, 10:42:06 AM »

Figured you guys may have come across this problem before.

Am trying to do an explode on an article word for word, replacing a certain word with a 'href'.
However after coding it came to the quick realization that exploding word for word and matching
it ends up putting any commas etc.. within the link.

Suppose I have this.

Code:
$string = "tattoo";
$url = "http://www.mydomain.com";
$article = "However, like any tattoo, it is important to choose a tattoo artist whom you can trust to do high quality artwork."

By Running this......

Code:
$article_exploded=explode(' ',$article);

        $article_new_array = array();
        foreach($article_exploded AS $exploded){
                if(stristr($exploded,$string)){
$article_new_array[] = "<a href='".$url."'>".$exploded."</a>";
}else{
$article_new_array[] = $exploded;
}
}

$article = implode(' ',$article_new_array);

echo $article;

I get this result.

"However, like any <a href=''>tattoo,[/url] it is important to choose a <a href=''>tattoo[/url] artist whom you can trust to do high quality artwork."

I need a way to get those commas out of the href tag.
Is it possible to delimit an explode with multiple delimiters??
Logged
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #1 on: May 21, 2009, 12:28:09 PM »

Why?

Just explode on ', '
Logged

hai
deregular
Expert
****
Offline Offline

Posts: 172


View Profile
« Reply #2 on: May 22, 2009, 12:36:51 AM »

Think you may have misunderstood VS.

I dont want explode by ', ' because I only want the 'one' word to be hyperlinked..
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: May 22, 2009, 11:41:31 AM »

Your request baffled me as well. Are you trying simply to replace a target word (like, in this case, "tattoo") with an href? To me it'd be very simple and I'd not explode it.

Code:
<?php

$inputBuff 
= (the text you want to modify)
$wordArr = array('tattoo''body art''ink');
foreach(
$wordArr as $word)
{
$url "<a href=\"http://www.target.com/\">$word</a>";
$newBuff str_replace($word$url$inputBuff);
}

?>


you can do this via preg_replace as well, there are a whole bunch of ways to do it - but I think str_replace is the fastest, if I'm reading you correctly.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #4 on: May 22, 2009, 03:37:43 PM »

If i read correctly he wants to make one of those plugins that replace certain words (or groups of words) in an article with avert links.

str_replace will be fastest.

but he is better to use a regexp since it will be more flexable.
That way he could match groups of words.
as in /(tattoo|body mark|ink mark)/i and make it case insensitive
he could then even take the word he matched and then replace it with different links.

I have no idea how to do regex in php and do not really care Smiley
Just talking about logic Smiley
Logged
deregular
Expert
****
Offline Offline

Posts: 172


View Profile
« Reply #5 on: May 23, 2009, 01:34:57 AM »

Sorry havent been back to this, it got sort of put on the boiler for abit.

But after revising, and looking at it again, my logic was to be understated ... wrong..
Sorry must have been one of those days when I was writing it.

I think a str_replace would be best also now after looking over it again.
I like your idea nop using regex since I can make it case insensitive.

Thanks all, much appreciated.
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!