p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: How to strip a string


Message #1 by "Wilson Sanchez" <wilsons@s...> on Tue, 12 Feb 2002 08:18:55 -0600
Hello Everyone:



I would like to know if there's a PHP function or a snippet somewhere that

let you get a string from a textbox and allows you to strip it each letter

into an array. For example: the user enters - Beginning PHP - and press the

submit button. I would like the output to be:



B

e

g

i

n

n

i

n

g



P

H

P



By traversing through a for loop, something like this:



( for $i=0; $i<= strlen($input); $i++ )

	print "<BR> $array[$i]";





Thank you in advance for your help.





Message #2 by "Nikolai Devereaux" <yomama@U...> on Tue, 12 Feb 2002 09:15:31 -0800

You can access any character in the string by its index or location into it

using a curly brace syntax.  It's similar to array indexing notation, so

your idea was pretty close.



for($i = 0; $i < strlen($your_string); ++$i)

{

    echo $your_string{$i} . "<br />\n";

}



Check out Example 6-3 in the php manual.

http://www.php.net/manual/en/language.types.string.php



If you needed it in an array for whatever reason, you could substitute this

line in your loop:



    $my_array[$i] = $your_string{$i};





take care,



nik





-----Original Message-----

From: Wilson Sanchez [mailto:wilsons@s...]

Sent: Tuesday, February 12, 2002 6:19 AM

To: beginning php

Subject: [beginning_php] How to strip a string





Hello Everyone:



I would like to know if there's a PHP function or a snippet somewhere that

let you get a string from a textbox and allows you to strip it each letter

into an array. For example: the user enters - Beginning PHP - and press the

submit button. I would like the output to be:



B

e

g

i

n

n

i

n

g



P

H

P



By traversing through a for loop, something like this:



( for $i=0; $i<= strlen($input); $i++ )

	print "<BR> $array[$i]";





Thank you in advance for your help.



Message #3 by "Krister" <krister@m...> on Tue, 12 Feb 2002 19:00:59 +0100
Hi

Yes, something like that. Fx. like this:



<html>

<head>

<title></title>

</head>

<body>

<form action="<?php echo $PHP_SELF; ?>" method="post">

<input type="text" name="txt">

<input type="submit">

</form>

<?php

 if ( isset( $txt ) ) {

 for ( $i = 0; $i < strlen( $txt ); $i++ )

  echo "$txt[$i]<br>";

 }

?>

</body>

</html>



Krister

----- Original Message -----

From: "Wilson Sanchez" <wilsons@s...>

To: "beginning php" <beginning_php@p...>

Sent: Tuesday, February 12, 2002 3:18 PM

Subject: [beginning_php] How to strip a string





> Hello Everyone:

>

> I would like to know if there's a PHP function or a snippet somewhere that

> let you get a string from a textbox and allows you to strip it each letter

> into an array. For example: the user enters - Beginning PHP - and press

the

> submit button. I would like the output to be:

>

> B

> e

> g

> i

> n

> n

> i

> n

> g

>

> P

> H

> P

>

> By traversing through a for loop, something like this:

>

> ( for $i=0; $i<= strlen($input); $i++ )

> print "<BR> $array[$i]";

>

>

> Thank you in advance for your help.

>

>

>




$subst('Email.Unsub').

>



Message #4 by "Wilson Sanchez" <wilsons@s...> on Tue, 12 Feb 2002 16:14:07 -0600
Thanks for all your help. All your examples were very helpful.



Once again, thank you.

Wilson



-----Original Message-----

From: Nikolai Devereaux [mailto:yomama@U...]

Sent: Tuesday, February 12, 2002 11:16 AM

To: beginning php

Subject: [beginning_php] RE: How to strip a string







You can access any character in the string by its index or location into it

using a curly brace syntax.  It's similar to array indexing notation, so

your idea was pretty close.



for($i = 0; $i < strlen($your_string); ++$i)

{

    echo $your_string{$i} . "<br />\n";

}



Check out Example 6-3 in the php manual.

http://www.php.net/manual/en/language.types.string.php



If you needed it in an array for whatever reason, you could substitute this

line in your loop:



    $my_array[$i] = $your_string{$i};





take care,



nik





-----Original Message-----

From: Wilson Sanchez [mailto:wilsons@s...]

Sent: Tuesday, February 12, 2002 6:19 AM

To: beginning php

Subject: [beginning_php] How to strip a string





Hello Everyone:



I would like to know if there's a PHP function or a snippet somewhere that

let you get a string from a textbox and allows you to strip it each letter

into an array. For example: the user enters - Beginning PHP - and press the

submit button. I would like the output to be:



B

e

g

i

n

n

i

n

g



P

H

P



By traversing through a for loop, something like this:



( for $i=0; $i<= strlen($input); $i++ )

	print "<BR> $array[$i]";





Thank you in advance for your help.








$subst('Email.Unsub').




  Return to Index