Page 1 of 1

PHP equivalent of a JavaScript command.

Posted: Thu May 04, 2006 11:40 am
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

Re: PHP equivalent of a JavaScript command.

Posted: Thu May 04, 2006 11:59 am
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);
?>

Posted: Thu May 04, 2006 12:20 pm
by Optimaximal
Thank you :)