homepage Welcome to WebmasterWorld Guest from 54.211.73.232
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
adding a number to a string
mikejs

5+ Year Member



 
Msg#: 4016202 posted 10:00 am on Oct 30, 2009 (gmt 0)

Hi I have a string red blue black 1 3 how would I add a number to the end of each block

so red4 blue4 black4 etc

and then after the string is sent the reciving end the number gets remove to leave the original values

red blue black etc

thanks

M

 

Zipper

10+ Year Member



 
Msg#: 4016202 posted 3:29 pm on Oct 30, 2009 (gmt 0)

well, there are many ways you could do this. some examples..

$yourString = "red blue black"'
$newString = "";
$words = explode(" ", $yourString);
foreach($words as $word) $newString .= $word . "4 ";

OR

str_replace(" ", "4 ", $yourString);

these are not perfect solutions, but enough to give you in idea of how to go about it.

mikejs

5+ Year Member



 
Msg#: 4016202 posted 3:54 pm on Oct 30, 2009 (gmt 0)

Thanks for the reply and then when I want to remove the number from the string I would do the reverse?

OutdoorWebcams

5+ Year Member



 
Msg#: 4016202 posted 4:14 pm on Oct 30, 2009 (gmt 0)

Is the numer of digits known?

If yes, you can use:


$originalstring = substr($string, 0, strlen($string)-$numberofdigits);
$number = substr($string, -$numberofdigits);

mikejs

5+ Year Member



 
Msg#: 4016202 posted 2:36 pm on Oct 31, 2009 (gmt 0)

Hi thanks for the reply this is the php I am trying to add this function to I want to add the extra didgit befoe the form is submitted and then on the server page I want to remove the digit

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

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4016202 posted 5:56 pm on Nov 1, 2009 (gmt 0)

I'm not sure of the directives here, but I **think** I am seeing what you are trying to do. If these values have no relevance to being read by humans, you may be better off using a delimiter so you get something like this.

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.

mikejs

5+ Year Member



 
Msg#: 4016202 posted 4:14 pm on Nov 3, 2009 (gmt 0)

Thanks for your reply this is what I have after i add the no 4 to the string

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

TheMadScientist

WebmasterWorld Senior Member themadscientist us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4016202 posted 6:16 am on Nov 4, 2009 (gmt 0)

I would do what rocknbil says and use a delimiter, because there is much less chance of unexpected results:

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.

mikejs

5+ Year Member



 
Msg#: 4016202 posted 10:42 am on Nov 4, 2009 (gmt 0)

thanks for the replies if I use:

$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

mikejs

5+ Year Member



 
Msg#: 4016202 posted 11:46 am on Nov 4, 2009 (gmt 0)

Ok so I have tried to impliment the above as follows

<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

TheMadScientist

WebmasterWorld Senior Member themadscientist us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4016202 posted 12:51 pm on Nov 4, 2009 (gmt 0)

There was just a little coding / typing error (omission) with the code posted, I think... I bolded it for you, because I made another change, so it's easier to throw a variable into the replace. (From the benchmarking scripts I've read, foreach() is one of the least optimal choices for anything, so I try to avoid it.)

$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.)

mikejs

5+ Year Member



 
Msg#: 4016202 posted 3:45 pm on Nov 4, 2009 (gmt 0)

Hi thanks for the reply the code above works fine it adds _4 to each array

output = yellow_4 3_4 5_4 300_4 1_4 2_4 3_4 5_4

now I want to reverse this

to get my original array back :-)

TheMadScientist

WebmasterWorld Senior Member themadscientist us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4016202 posted 9:37 pm on Nov 4, 2009 (gmt 0)

That's easier...
(I didn't test this one, so you might need to adjust slightly, but probably not.)

$yourVar='4';
$yourReplace='_'.$yourVar;
$yourString = "red_4 blue_4 black_4";

$yourString=str_replace($yourReplace,'',$yourString);

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved