Find character in a php array

Asked

Viewed 556 times

3

Imagining that I have the following array:

$o_meu_array = array("123_rui", "125_joao", "287_manuel");

Now I want to find inside my array "123". When I find 123, I want to replace 123 with another value given by the user. For example, if the user enters "10", it will become "10_rui". You can do this?

  • either the code or the path of the stones ?

  • if possible the code otherwise all comments are welcome. thanks

1 answer

5


To replace values in a array you can use the function preg_replace()

$meu_array = array("123_rui", "125_joao", "287_manuel");
$procurar = "/123/";
$substituir = "10";
$meu_array = preg_replace($procurar, $substituir, $meu_array);

Example

  • That was it. Now just one question, is it possible to search for 123 and then replace the name? for example: find: 123 when find replaces the name with Joaquim result: $meu_array = array("123_joaquim", "125_joao", "287_manuel");

  • 2

    @pc_oc That would be https://ideone.com/vGCmko ?

  • yes, that’s right! Thank you very much!

  • if the regular expression were with numbers it would look something like this: $search = "/123_([0-9]|[1-9][0-9]|[1-9][0-9][0-9])*/"; right? thanks again

  • 1

    @pc_oc that you would do /123_([0-9])*/, or 123_qualquer_numero_qualquer_size, you can accept letters, numbers, characters, etc., for more information about regular expressions see this link http://www.ibm.com/developerworks/br/library/os-php-regex1/

  • thanks again!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.