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

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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 !

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.