PHP equivalent of a JavaScript command.

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
Optimaximal
Forum Newbie
Posts: 3
Joined: Thu May 04, 2006 11:31 am

PHP equivalent of a JavaScript command.

Post by Optimaximal »

Hi all, new here :)

I have a javascript function designed to search a string returned by a PHP chatbot (response) for a specific value before stopping and then outputting everything that came before it (i.e. say the string was 'Hello how are you' and it was told to look for 'are' then it would output 'Hello how ').

It uses the indexOf function and looks something like this -

Code: Select all

<script>
	var response= "' . fixquote($botresponse->response) . '"; 
	var position = 0;
	var chat = "";
	function Talk()
	{ 
	position = response.indexOf("<br>");
		if(position > -1) 
		{
			chat = response.substring(0,position);
		}
		else
		{
			chat = response;
		}
	}  		   
	Talk();
</script>
I'm pretty much a n00b at anything in PHP apart from MySQL data manipulation so i was wondering if anyone knows the equivalent PHP command (if one exists).

I need it because certain responses from the chatterbot include JavaScript and break the function above when you nest <script> tags inside each other.

Any help is appreciated. Thanks - Andrew
Last edited by Optimaximal on Thu May 04, 2006 12:02 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP equivalent of a JavaScript command.

Post by Christopher »

Something like:

Code: Select all

<?php
	function talk($response)
	{ 
	$position = strpos($response, "<br>");
		if($position !== false) 
		{
			$chat = substr($response, 0,$position);
		}
		else
		{
			$chat = $response;
		}
	echo($chat);
	}  		   

	$response= "' . addslashes($botresponse->response) . '"; 
	talk($response);
?>
(#10850)
Optimaximal
Forum Newbie
Posts: 3
Joined: Thu May 04, 2006 11:31 am

Post by Optimaximal »

Thank you :)
Post Reply