binumathew Posted January 22, 2010 Share Posted January 22, 2010 Hi Friends I Have a string "The comfortable seating, wide $automatic doors in the middle for entry, exit at the front, advanced passenger information $system and $facilities for the physically $challenged came in for praise. The stop switches at the posts in the bus for the $commuters to request for halt and the LCD destination boards in Malayalam and English drew appreciation." In the above string, there is some strings which are starting with "$" symbol. i need the place a link where $ symbol is made. How can i do that can you guys please help me Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/ Share on other sites More sharing options...
RichardRotterdam Posted January 22, 2010 Share Posted January 22, 2010 maybe these functions are what you are looking for: preg_replace str_replace Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-999961 Share on other sites More sharing options...
binumathew Posted January 22, 2010 Author Share Posted January 22, 2010 i tried, i am not well in php string function, can you help me out, by make a small snippet for me? Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-999966 Share on other sites More sharing options...
RichardRotterdam Posted January 22, 2010 Share Posted January 22, 2010 What have you tried? Can you show the code? Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-999980 Share on other sites More sharing options...
binumathew Posted January 22, 2010 Author Share Posted January 22, 2010 Sorry DJ, I tried so much, i got anger and i delete the code, its making me pain in my ass Can you gave me a code snippet for that ?? Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-999982 Share on other sites More sharing options...
RichardRotterdam Posted January 22, 2010 Share Posted January 22, 2010 There are examples on how to use these functions in the manual try those Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-999989 Share on other sites More sharing options...
binumathew Posted January 22, 2010 Author Share Posted January 22, 2010 Thks, i that case, there is no need of forums, right man? Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-999990 Share on other sites More sharing options...
laffin Posted January 23, 2010 Share Posted January 23, 2010 although str_replace, and preg are much preferred, there is also eval, since your string is php compliant, all you need to do assign the variables first create a simple eval statement <?php $str='The comfortable seating, wide $automatic doors in the middle for entry, exit at the front, advanced passenger information $system and $facilities for the physically $challenged came in for praise. The stop switches at the posts in the bus for the $commuters to request for halt and the LCD destination boards in Malayalam and English drew appreciation.'; $automatic = 'pull'; $system = 'nintendo console'; $facilities = 'porta-potty'; $challenged = 'impaired'; $commuters = 'passengers'; eval("\$str = \"$str\";"); echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-1000226 Share on other sites More sharing options...
binumathew Posted January 23, 2010 Author Share Posted January 23, 2010 Thks for the replay But it is not my needed answer I need Links in the string... Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-1000323 Share on other sites More sharing options...
binumathew Posted January 23, 2010 Author Share Posted January 23, 2010 The Code i used is <?php $str=$_REQUEST['tes']; $newStr=""; $t=0; for($i=0;$i<strlen($str);$i++) { if($str[$i]=='$') { $t=1; $newStr=$newStr."<a href='$link'>"; } else if($str[$i]==" " && $t=1) {$link=$newStr; $newStr=$newStr."</a>"; $t=0; } $newStr=$newStr.$str[$i]; } echo "New $newStr"; ?> This is not making link i need link like if the user put string like "Welcome Fellows to My $world"; i need a link at $world, and link to $world <a href="$world">$world</a> Like this Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-1000361 Share on other sites More sharing options...
bl00dshooter Posted January 23, 2010 Share Posted January 23, 2010 this ? <?php $str='The comfortable seating, wide $automatic doors in the middle for entry, exit at the front, advanced passenger information $system and $facilities for the physically $challenged came in for praise. The stop switches at the posts in the bus for the $commuters to request for halt and the LCD destination boards in Malayalam and English drew appreciation.'; $str = str_replace('$automatic',"http://www.google.com",$str); $str = str_replace('$system',"http://www.phpfreaks.com",$str); $str = str_replace('$facilities',"http://www.redtube.com",$str); $str = str_replace('$challenged',"http://www.pornhub.com",$str); $str = str_replace('$commuters',"http://www.youporn.com",$str); echo $str; ?> or this ? <?php $str='The comfortable seating, wide $automatic doors in the middle for entry, exit at the front, advanced passenger information $system and $facilities for the physically $challenged came in for praise. The stop switches at the posts in the bus for the $commuters to request for halt and the LCD destination boards in Malayalam and English drew appreciation.'; $str = str_replace('$',"http://www.google.com",$str); echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-1000370 Share on other sites More sharing options...
Daniel0 Posted January 23, 2010 Share Posted January 23, 2010 Thks, i that case, there is no need of forums, right man? I think you've got some misconceptions about both the purpose of this forum and how learning works. This forum is not made so people can write code for you on-demand, and learning does not work by having others do it for you. Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-1000372 Share on other sites More sharing options...
binumathew Posted January 23, 2010 Author Share Posted January 23, 2010 this ? <?php $str='The comfortable seating, wide $automatic doors in the middle for entry, exit at the front, advanced passenger information $system and $facilities for the physically $challenged came in for praise. The stop switches at the posts in the bus for the $commuters to request for halt and the LCD destination boards in Malayalam and English drew appreciation.'; $str = str_replace('$automatic',"http://www.google.com",$str); $str = str_replace('$system',"http://www.phpfreaks.com",$str); $str = str_replace('$facilities',"http://www.redtube.com",$str); $str = str_replace('$challenged',"http://www.pornhub.com",$str); $str = str_replace('$commuters',"http://www.youporn.com",$str); echo $str; ?> or this ? <?php $str='The comfortable seating, wide $automatic doors in the middle for entry, exit at the front, advanced passenger information $system and $facilities for the physically $challenged came in for praise. The stop switches at the posts in the bus for the $commuters to request for halt and the LCD destination boards in Malayalam and English drew appreciation.'; $str = str_replace('$',"http://www.google.com",$str); echo $str; ?> Thks Blood Shooter. But the User is gaving input, so it can be change, i need something like twitter method, all the tag in # becaming link to search/#tag, something like that Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-1000380 Share on other sites More sharing options...
binumathew Posted January 23, 2010 Author Share Posted January 23, 2010 Thks, i that case, there is no need of forums, right man? I think you've got some misconceptions about both the purpose of this forum and how learning works. This forum is not made so people can write code for you on-demand, and learning does not work by having others do it for you. Sorry Daniel0, I dont mean that. i am here for learning purpose...Sorry and i will not repeat it.... Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-1000381 Share on other sites More sharing options...
bl00dshooter Posted January 23, 2010 Share Posted January 23, 2010 Sorry for the long mate, was drinking a bit Now, this ? <?php $str = '$google'; $lenstr = strlen($str); if($str[0] == '$') { $strlink = "<a href=\"http://www.".str_replace('$','',$str).".com\">".str_replace('$','',$str)."</a>"; echo $strlink; } else { echo str_replace('$','',$str); } ?> if you put a '$' before the word, it will be a link to http://www.+word+.com and the text will be word. Any problems, just post. Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-1000396 Share on other sites More sharing options...
bl00dshooter Posted January 23, 2010 Share Posted January 23, 2010 No edit button, so: I finally made the code perfect, as you wanted ! <?php $str = 'The comfortable seating, wide $automatic doors in the middle for entry, exit at the front, advanced passenger information $system and $facilities for the physically $challenged came in for praise. The stop switches at the posts in the bus for the $commuters to request for halt and the LCD destination boards in Malayalam and English drew appreciation.'; $str2 = explode(" ",$str); $strwords = str_word_count($str); for($i=0;$i<$strwords;$i++) { if($str2[$i][0] =='$') { $replaced = str_replace('$','',$str2[$i]); echo "<a href=\"http://www.$replaced.com\">$replaced</a> "; } else { echo $str2[$i]." "; } } ?> Use this code, instead of the one I posted above. It will convert: The comfortable seating, wide $automatic doors in the middle for entry, exit at the front, advanced passenger information $system and $facilities for the physically $challenged came in for praise. The stop switches at the posts in the bus for the $commuters to request for halt and the LCD destination boards in Malayalam and English drew appreciation. Into: The comfortable seating, wide <a href="http://www.automatic.com">automatic</a> doors in the middle for entry, exit at the front, advanced passenger information <a href="http://www.system.com">system</a> and <a href="http://www.facilities.com">facilities</a> for the physically <a href="http://www.challenged.com">challenged</a> came in for praise. The stop switches at the posts in the bus for the <a href="http://www.commuters.com">commuters</a> to request for halt and the LCD destination boards in Malayalam and English drew appreciation. Enjoy ! Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-1000418 Share on other sites More sharing options...
Andy-H Posted January 23, 2010 Share Posted January 23, 2010 <?php $str = 'The comfortable seating, wide $automatic doors in the middle for entry, exit at the front, advanced passenger information $system and $facilities for the physically $challenged came in for praise. The stop switches at the posts in the bus for the $commuters to request for halt and the LCD destination boards in Malayalam and English drew appreciation.'; $arr = str_word_count($str, 1, '$,.\''); foreach($arr as $k => $val) : if ( substr($val, 0, 1) == '$' ) { $arr[$k] = '<a href="' . $val . '">' . $val . '</a>'; } endforeach; $str = implode(" ", $arr); echo $str; ?> Lol I did it a different way but was unclear as to what you wanted. Outputted: The comfortable seating, wide <a href="$automatic">$automatic</a> doors in the middle for entry, exit at the front, advanced passenger information <a href="$system">$system</a> and <a href="$facilities">$facilities</a> for the physically <a href="$challenged">$challenged</a> came in for praise. The stop switches at the posts in the bus for the <a href="$commuters">$commuters</a> to request for halt and the LCD destination boards in Malayalam and English drew appreciation. Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-1000434 Share on other sites More sharing options...
binumathew Posted January 23, 2010 Author Share Posted January 23, 2010 Thks Andy-H and bl00dshooter, That code makes a perfect answer for my question... Thks Very Much... Quote Link to comment https://forums.phpfreaks.com/topic/189417-finding-a-char-from-string-and-make-that-link/#findComment-1000441 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.