Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
[How to] Exclude replacement inside hyperlinks, maybe using if statement,or other way
Old 04-19-2011, 07:04 PM [How to] Exclude replacement inside hyperlinks, maybe using if statement,or other way
Skilled Talker

Posts: 78
Name: syu
Trades: 0
Hi guys, below are simple script for word replacement, it is replacing a word in to link



PHP Code:
$word = array(
'google',
'yahoo'
);

$link = array(
'<a href="http://google.com">google</a>',
'<a href="http://yahoo.com">yahoo</a>'
);


$this->post['message'] = str_ireplace($word$link$this->post['message']); 






Quote:
for example text
Quote:
i love google result
the output printed will be like this
HTML Code:
i love <a href="http://google.com">google</a> result
it is just replacing google text into a link like this <a href="http://google.com">google</a>






Quote:
but the problem is if the original google text is already inside a link, like this
HTML Code:
<a href="http://google.com">i love google result</a>
the output printed will be broken like this, there will be double hyperlink
HTML Code:
<a href="http://google.com">i love <a href="http://google.com">google</a> result</a>





please help guys, how to exclude the text google inside hyperlinks above from replaced, but the text google outside hyperlinks are still replaced

maybe using if statement in the simple script above, or other way
__________________
Lets help each other
basketmen is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-20-2011, 12:59 AM Re: [How to] Exclude replacement inside hyperlinks, maybe using if statement,or other
shivaji's Avatar
Ultra Talker

Posts: 321
Trades: 0
Maybe you can use strip_tags to remove all a tags from message and then insert your own tags.
http://php.net/manual/en/function.strip-tags.php
__________________

Please login or register to view this content. Registration is FREE
- uncommon free scripts

Please login or register to view this content. Registration is FREE
- Städte, Sport, Party, Gourment, Apartments, Hotels
shivaji is offline
Reply With Quote
View Public Profile Visit shivaji's homepage!
 
Old 04-20-2011, 10:18 PM Re: [How to] Exclude replacement inside hyperlinks, maybe using if statement,or other
Skilled Talker

Posts: 78
Name: syu
Trades: 0
Quote:
Originally Posted by shivaji View Post
Maybe you can use strip_tags to remove all a tags from message and then insert your own tags.
http://php.net/manual/en/function.strip-tags.php
i dont know how to do it from above simple script, can you guys give example please?
__________________
Lets help each other
basketmen is offline
Reply With Quote
View Public Profile
 
Old 04-22-2011, 04:29 AM Re: [How to] Exclude replacement inside hyperlinks, maybe using if statement,or other
Super Spam Talker

Posts: 880
Name: Paul W
Trades: 0
Quote:
Originally Posted by basketmen View Post
i dont know how to do it from above simple script, can you guys give example please?
There's an example on the page cited. Furthermore, if you can't knock up a two line script to test it you shouldn't be writing code - have a go, ffs.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is online now
Reply With Quote
View Public Profile
 
Old 04-22-2011, 07:42 AM Re: [How to] Exclude replacement inside hyperlinks, maybe using if statement,or other
shivaji's Avatar
Ultra Talker

Posts: 321
Trades: 0
I don't know nothing about OOP but I can try.

PHP Code:
$word = array(
'google',
'yahoo'
);

$link = array(
'<a href="http://google.com">google</a>',
'<a href="http://yahoo.com">yahoo</a>'
);

$this->post['message'] =  strip_tags($this->post['message']); 
$this->post['message'] = str_ireplace($word$link$this->post['message']); 
__________________

Please login or register to view this content. Registration is FREE
- uncommon free scripts

Please login or register to view this content. Registration is FREE
- Städte, Sport, Party, Gourment, Apartments, Hotels
shivaji is offline
Reply With Quote
View Public Profile Visit shivaji's homepage!
 
Old 04-28-2011, 11:19 AM Re: [How to] Exclude replacement inside hyperlinks, maybe using if statement,or other
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Try using preg_replace(). It's a combination of the replace function and regular expressions. Which means, you can use regual expressions to just match the exact word "google", and not just anything containing the word.

You can look more into regular expressions if you're interested, but I think this should do it.

PHP Code:
$search = array("/^google$/i""/^yahoo$/i");
$replace = array('<a href="http://www.google.com">google</a>''<a href="http://www.yahoo.com">yahoo</a>');

$this->post['message'] = preg_replace($search$replace$this->post['message']); 
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to [How to] Exclude replacement inside hyperlinks, maybe using if statement,or other way
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.00448 seconds with 12 queries