Page 1 of 1

Five GREAT PHP STRING PROBLEMS

Posted: Sun Oct 31, 2010 7:09 am
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

Re: Five GREAT PHP STRING PROBLEMS

Posted: Sun Oct 31, 2010 7:22 am
by josh
str_replace(), preg_match(), preg_match_all(), preg_replace(), etc.. all these functions address 1 or more of your problems.

Re: Five GREAT PHP STRING PROBLEMS

Posted: Sun Oct 31, 2010 11:47 am
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