Forum Moderators: coopster
$yourString = "red blue black"'
$newString = "";
$words = explode(" ", $yourString);
foreach($words as $word) $newString .= $word . "4 ";
str_replace(" ", "4 ", $yourString);
its a php client server socket
<?php
if(isset($_POST['submit'])) {
$str = $_POST["fname"];
$str .= $_POST["fname1"];
$str .= $_POST["fname2"];
$str .= $_POST["fname3"];
$str .= $_POST["fname4"];
$str .= $_POST["fname5"];
//echo $str;
do_it($str);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="fname" value="" >
<input type="text" name="fname1" value="" >
<input type="text" name="fname2" value="" >
<input type="text" name="fname3" value="" >
<input type="text" name="fname4" value="" >
<input type="text" name="fname5" value="" >
<input type="submit" name="submit" value="Click">
</form>
<?php
function do_it($str)
{
// set some variables
$host = "localhost";
$port = 19;
$ret;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$ret = socket_connect($socket, $host, $port);
//$str = $str . "\n";
socket_write($socket, $num);
$input = socket_read($socket,1024);
$input = trim($input);
echo $input;
socket_close($socket);
}
?>
fname_1
fname_3443
fname2_867
Or use a dash, anything else.
The first reason is in the third example. Note digits on either side of the delimiter. The leading part of the identifier can be any character other than the delimiter, so you can always combine and re-split/explode the values reliably. The second reason is you don't have to be concerned at all about the number of digits in the numeric value.
This approach comes in handy when editing data - you get your unique record identifier from the database (id, user_id, whatever) and that is the number you add to the right of your delimiter. In many cases, this avoids looping through a hard-coded iteration.
original output
Array
(
[0] => green
[1] => 2
[2] => 3
[3] => 200
[4] => 1
[5] => 2
[6] => 3
[7] => 4
green4 24 34 2004 14 24 34 44 the result is as expected now the values in the array might change is there a way to search for say no 4 and then remove that char
Add the delimiter and number 4
str_replace(" ", "_4 ", $yourString);
To reverse it:
$yourString=implode(" ",$array);
Remove the delimiter and number 4
str_replace("_4 ", " ", $yourString);
$array=explode(" ",$yourString);
It's essentially the same as what you are doing, but as I said it's less likely to provide unexpected results, and as rocknbil said, makes it so the value can be changed reliably... If you are set on doing it without a delimiter, remove the _ from the example above.
$newString = "";
$words = explode(" ", $str);
foreach($words as $word) $newString .= $word . "_7 ";
echo $newString;
I get expected results
blue_7 3_7 5_7 300_7 retr_7 trretre_7 trretre_7 tretr_7
I tried str_replace(" ", "_4 ", $yourString); but it did not apear to eddit the string
so this is the string being sent over
blue_7 3_7 5_7 300_7 retr_7 trretre_7 trretre_7 tretr_7
in this var $newString
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['submit'])) {
$str = $_POST['fname'].' '. $_POST['fname1'].' '. $_POST['fname2'].' '.$_POST['fname3'].' '.$_POST['fname4'].' '.$_POST['fname5'].' '.$_POST['fname6'].' '.$_POST['fname7'];
//echo $str;
$newString = '';
$words = explode(' ', $str);
foreach($words as $word) $newString .= $word . '_7 ';
echo $newString;
$exp_input = explode(' ', $newString);
echo '<pre>';
print_r($exp_input);
echo '</pre>';
do_it($str);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<table width="412" border="0" cellpadding="4">
<tr>
<td width="111"><label>
Table Backround<br>
</label>
<label>
<input type="radio" name="fname" value="blue" id="TableColour_0">
Blue</label>
<br>
<label>
<input type="radio" name="fname" value="green" id="TableColour_1">
Green</label>
<br>
<label>
<input type="radio" name="fname" value="yellow" id="TableColour_2">
Yellow</label>
<br>
<label>
<input type="radio" name="fname" value="orange" id="TableColour_3">
Orange</label></td>
<td width="81">Table Border<br>
<label>
<input type="radio" name="fname1" value="1" id="fname2_0">
1</label>
<br>
<label>
<input type="radio" name="fname1" value="2" id="fname2_1">
2</label>
<br>
<label>
<input type="radio" name="fname1" value="3" id="fname2_2">
3</label>
<br>
<label>
<input type="radio" name="fname1" value="4" id="fname2_3">
4</label></td>
<td width="93">Table Padding<br>
<label>
<input type="radio" name="fname2" value="1" id="fname2_0">
1</label>
<br>
<label>
<input type="radio" name="fname2" value="3" id="fname2_1">
3</label>
<br>
<label>
<input type="radio" name="fname2" value="5" id="fname2_2">
5</label><br>
<label>
<input type="radio" name="fname2" value="7" id="fname2_3">
7</label></td>
<td width="85"> Table width<br>
<label>
<input type="radio" name="fname3" value="150" id="fname2_4">
150px</label>
<br>
<label>
<input type="radio" name="fname3" value="200" id="fname2_5">
200px</label>
<br>
<label>
<input type="radio" name="fname3" value="300" id="fname2_6">
300px</label>
<br>
<label>
<input type="radio" name="fname3" value="400" id="fname2_7">
400px</label></td>
</tr>
</table>
<p>
<input type="text" name="fname4" value="" >
<input type="text" name="fname5" value="" >
<input type="text" name="fname6" value="" >
<input type="text" name="fname7" value="" >
<input type="submit" name="submit" value="Click">
</p>
</form>
<?php
function do_it($str)
{
// set some variables
$host = "localhost";
$port = 6144;
$ret;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$ret = socket_connect($socket, $host, $port);
//$str = $str . "\n";
socket_write($socket, $str);
$input = socket_read($socket,1024);
$input = trim($input);
echo $input;
socket_close($socket);
}
?>
</body>
</html>
Server
<html>
<head>
</head>
<body>
<?php
// set some variables
$host = "localhost";
$port = 6144;
// don't timeout!
set_time_limit(0);
do{
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
$exp_input = explode(' ', $input);
$input = implode(" ",$array);
//Remove the delimiter and number 4
str_replace("_4 ", " ", $input);
$exp_input = explode(' ', $input);
// clean up input string
//$input = trim($input);
$output = "<table bgcolor=\"" . $exp_input[0] . "\" border=\"" .$exp_input[1]. "\"cellpadding=\"" .$exp_input[2]. "\" width=\"" . $exp_input[3]. "\">\n";
"<tr><td>\"" . $exp_input[4] . "\" </td><td>\"" . $exp_input[5] . "\"</td></tr>\n";
"<tr><td>\"" . $exp_input[6] . "\" </td><td>\"" . $exp_input[7] . "\"</td></tr>\n";
//$z = 3;
//while ($z < count($array)) {
//$output .= "<tr><td>" . $exp_input[$z] . "</td>";
//$output .= "<td>" . $exp_input[$z + 2] . "</td></tr>\n";
//$z++;
//}
$z = 4;
for ($i=0; $i < 2; $i++) {
$output .= "<tr><td>" . $exp_input[$z] . "</td>";
$output .= "<td>" . $exp_input[$z + 2] . "</td></tr>\n";
$z++;
}
$output .= "</table>\n";
//socket_write($socket, $exp_input);
socket_write($spawn, $output, strlen($output)) or die("Could not write output\n");
//echo $input;
socket_close($socket);
// close sockets
}while ($input != "END");
?>
</body>
</html>
but the client just hangs as though it does not know what to do
any suggestions
$yourVar='4';
$yourReplace='_'.$yourVar.' ';
$yourString = "red blue black";
if(strrpos($yourString,' ')!==strlen($yourString)-1) { $yourString.=" "; }
$yourString=str_replace(' ',$yourReplace,$yourString);
echo $yourString;
I even tested before posting for once :)
(Not sure on your last question... I haven't been following the thread close enough to know where you are or what you are really doing. Sry.)