0

I have a string, how do I convert it to an array?

After manipulating that array, how do I again make it into a string?

Do strings in PHP behave the same way as in Java?

is there a dupe for this?

2
  • 1
    I would encourage you to check out the rich library of PHP string manipulation functions before you end up trying to write your own: PHP has a lot of built in functions, and I've often found that what I need has already been written. Commented Jun 23, 2009 at 13:58
  • If you need a multi-byte solution, look elsewhere. Commented Dec 7, 2019 at 16:52

9 Answers 9

7

as in C, strings are arrays in php

then

<?php
$a = "hola";

for($i=0; $i < strlen($a); $i++) {
 echo $a[$i] . "\n";

}

$a[2] = "-"; // will print ho-a
?>

what operation do you want to do?

Sign up to request clarification or add additional context in comments.

6 Comments

if they behave link C strings that will do. I want to filter out certain characters out of a string (not all occurences). thanks.
i think explode and implode would be the more performant solution.
In this instance, if one wants to edit specific characters of a string then I think treating it like a character array may be easier and certainly more elegant.
I know this isn't performant.. but what I wanted to show.. is the WAY you can drive arrays in php
Strings are not arrays at all. However, in many ways they can be treated as such.
|
5
explode ( string $delimiter , string $string [, int $limit ] )

... and after changes ...

implode ( string $glue , array $pieces )

check out http://php.net/explode
and http://php.net/implode

You can also use split or join which, as far as I know, support regex

1 Comment

what to write in $delimiter and $glue if i want to break into just single characters and back?
1
$wordArray = str_split('string to array');

Comments

0

in php you can use:

split like

Description

array split ( string $pattern , string $string [, int $limit ] )

Splits a string into array by regular expression.

or

implode

<?php

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

?> 

Comments

0

à la perl

<?php
    $string = 'aslkdjlcnasklhdalkfhlasierjnalskdj';
    $array  = array_slice(preg_split('//sx', $string), 1, -1);
?>

Comments

0

In Java you can do String.tocharArray() which converts the string into an array of characters. You can do a String.split(regex) to split by a regular expression, returning a String array. A char array or String array can be looped on easily to convert back to a string.

I'm not sure what you mean by "do they behave the same". Essentially they provide the same functionality... however in Java a String is an object that can be iterated upon if need be.

1 Comment

i mean like in C/C++ they are actually arrays and no such thing as a string like in java. so whats the situation in php. I think Gabriel Sosa did it. thanks
0

In PHP Strings can be accessed like arrays.

e.g.:

$my_string = 'abcdef';

$my_string[0] ==> 'a'
$my_string[1] ==> 'b'

If you want to convert a series of words into an array then use explode(' ',$my_string);

Note: Strings in PHP are not the same as Java. In PHP they can also represent the contents of any file.

You can learn almost anything there is to know by checking the documentation :)

1 Comment

exploding by ' ' <space doesn't do and exploding by '' <no-space> gave an error. documentation becomes too tacky for me to understand. Human explaining touch makes it more easier to learn. ;)
0

In PHP you can split and join. Don´t know how Java behaves.

1 Comment

Ahh, maybe you meant what Gabriel Sosa is talking about ^^ then don´t mind my answer =P
0

For all those looking for a simple multi-byte solution ...

Remember, set the character encoding appropriately for your task.

mb_internal_encoding('UTF-8');  // For example.
mb_regex_encoding('UTF-8');     // For example.

String to Array

$characters = mb_split("", $string); // Returns an array.

Array to String

$newString = implode($charaters);

Your other option would be to loop through $characters and concatenate the string yourself!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.