How to stick with just one part of the string?

Asked

Viewed 94 times

5

I have several string’s (idPergunta-18, idResposta-18, idPergunta-17, idResposta-17)

Needed to write only (idPergunta, idResposta, idPergunta, idResposta)

How do I in PHP?

The goal is as follows:

Right now I have the following array coming from a $_POST

array(28) { ["privacidade-16"]=> string(1) "1" ["verificada-16"]=> string(1) "1" ["idPergunta-16"]=> string(2) "16" ["resposta-16"]=> string(5) "vitor" ["privacidade-17"]=> string(1) "1" ["verificada-17"]=> string(1) "1" ["idPergunta-17"]=> string(2) "17" ["resposta-17"]=> string(10) "2015-10-09" ["privacidade-18"]=> string(1) "1" ["verificada-18"]=> string(1) "1" ["idPergunta-18"]=> string(2) "18" ["idRespostaPre-18"]=> string(2) "19" ["privacidade-19"]=> string(1) "1" ["verificada-19"]=> string(1) "1" ["idPergunta-19"]=> string(2) "19" ["resposta-19"]=> string(3) "Rua" ["privacidade-20"]=> string(1) "1" ["verificada-20"]=> string(1) "1" ["idPergunta-20"]=> string(2) "20" ["idRespostaPre-20"]=> string(2) "22" ["privacidade-21"]=> string(1) "1" ["verificada-21"]=> string(1) "1" ["idPergunta-21"]=> string(2) "21" ["resposta-21"]=> string(4) "4465" ["privacidade-30"]=> string(1) "1" ["verificada-30"]=> string(1) "1" ["idPergunta-30"]=> string(2) "30" ["idRespostaPre-30"]=> array(4) { [0]=> string(3) "186" [1]=> string(3) "187" [2]=> string(3) "188" [3]=> string(3) "189" } }

What I need is to separate this array into an array with several arrays inside and I would have to separate it as follows

an array with: array[0] = ["privacy"]=> string(1) "1" ["verified"]=> string(1) "1" ["idPergunta"]=> string(2) "16" ["reply"]=> string(5) "Vitor"

array[1] = ["privacy-17"]=> string(1) "1" ["verified-17"]=> string(1) "1" ["idPergunta"]=> string(2) "17" ["reply"]=> string(10) "2015-10-09"

array[2] = ["privacy"]=> string(1) "1" ["verified"]=> string(1) "1" ["idPergunta"]=> string(2) "18" ["idRespostaPre"]=> string(2) "19"

from now on, this because the fields that come from the POST are dynamic or today are 10 but tomorrow can be 20

  • all thus separated by a trace?

  • This issue seems to me another question.

3 answers

6


Can replace with a regular expression, which matches -[0-9], use the function preg_replace() for that reason.

If the string is only idPergunta-18, utilize strstr() and inform the third argument($before_needle) as true, this function finds the first occurrence of the delimiter and divides it, by default the return is the right part of the delimiter, if you want to change this inform the third argument.

<?php 
//opção 1
$str = '(idPergunta-18, idResposta-18, idPergunta-17, idResposta-17)';
echo  preg_replace('/\-[0-9]+/', '', $str);
//opção 2
echo strstr('idPergunta-18', '-', true);

Exit:

(idPergunta, idResposta, idPergunta, idResposta)
idPergunta
  • I think the idea was to treat string by string.

  • @Jorgeb. I don’t understand

  • 1

    guy idPergunta-18 => idPergunta

  • @Jorgeb. the second option seems appropriate?

  • Yes, it seems to me.

2

You can also solve by taking the position of the delimiter '-' with strripos() and just cut what you need with substr().

Example:

$palavra = 'idPergunta-18';
$posicaoDeCorte = strripos($palavra, '-');
echo substr($palavra, 0,$posicaoDeCorte);

See working on Ideone

1

I do not know what purpose you have with this, but already leaving in a way that opens several possibility to work.

Code

preg_match_all("~(id\w+)-(\d+)~", $str, $matches);

$matches[1]; // ARRAY DE PERGUNTA / RESPOSTA
$matches[2]; // ARRAY DE ID

Displaying

echo implode(' , ', $matches[2]); // idPergunta, idResposta, idPergunta, idResposta

Relating

foreach ($matches[1] as $k => $value){
    $name = $value;
    $id = $matches[2][$k];
}

Browser other questions tagged

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