Five GREAT PHP STRING PROBLEMS

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dentrite
Forum Newbie
Posts: 16
Joined: Thu Jun 17, 2010 7:10 am

Five GREAT PHP STRING PROBLEMS

Post by dentrite »

Any body please answere these :
1. there is a variable which may or may not have a url link in between text, if there is a url it has to be stored in a variable $url. text before link shall be stored in before and after text in $after.

Code: Select all

$var=$_POST['text'];
for example if

Code: Select all

$var="hi, http://google.com is a good site ";
then $url should print "http://google.com" $before should print "hi, " and $after should print " is a good site ";

2. how can i replace html markups (symbols] by suitable code in same variable? ex. > should be replaced by & g t;

3. how can I search for the existence of a word in above variable?. for ex. if $tobesearched="good" in above $var

4. on run time how to add a word before/after a particular word in above variable $var. for ex. after adding < b > before 'good', $var should become $var="hi, http://google.com is a < b >good site ";

5. on run time how to remove a particular word ?


IF YOU HAVE A WORKING SOLUTION PLEASE POST
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Five GREAT PHP STRING PROBLEMS

Post by josh »

str_replace(), preg_match(), preg_match_all(), preg_replace(), etc.. all these functions address 1 or more of your problems.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Five GREAT PHP STRING PROBLEMS

Post by s.dot »

2. how can i replace html markups (symbols] by suitable code in same variable? ex. > should be replaced by & g t;
htmlentities()
3. how can I search for the existence of a word in above variable?. for ex. if $tobesearched="good" in above $var
if (strpos($var, 'word') !== false){ word was found } (or stripos())
4. on run time how to add a word before/after a particular word in above variable $var. for ex. after adding < b > before 'good', $var should become $var="hi, http://google.com is a < b >good site ";
str_replace('good', '<b>good'); (or str_ireplace)
5. on run time how to remove a particular word ?
str_replace (or str_ireplace) or preg_replace if you want to get preceding or trailing white space
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply