Jump to content

Finding a Char from string and make that Link


binumathew

Recommended Posts

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

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;
?>

 

 

 

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

 

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, 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.

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

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....  :-[

Sorry for the long mate,

was drinking a bit  8)

 

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.

 

 

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 !

<?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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.