How to stick with only one part of the string? [duplicate]

5

I have several string's (id-question-18, id-answer-18, id-question-17, id-answer-17)

I needed to just write (id Question, id Answer, id question, id answer)

How do I do in PHP?

The goal is as follows:

At this point I have the following array that comes 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 multiple arrays inside and would have to separate it as follows

an array with: array [0] = ["privacy"] = > string (1) "1" ["verified"] = > string (1) "1" ["questionID"] = > string (2) "16" ["answer"] = > string (5) "vitor"

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

array [2] = ["privacy"] = > string (1) "1" ["verified"] = > string (1) "1" ["questionID"] = > string (2) "18" ["idResetPre"] = > string (2) "19"

Thereafter, this is because the fields that come from the POST are dynamic or are today 10 but tomorrow may be 20

    
asked by anonymous 27.10.2015 / 17:11

3 answers

6

You can do a replace with a regular expression, which combines -[0-9] , use the preg_replace() function for this.

If the string is only idPergunta-18 , use strstr () and enter 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 it, enter 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);

Output:

(idPergunta, idResposta, idPergunta, idResposta)
idPergunta
    
27.10.2015 / 17:17
2

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

Example:

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

See working at Ideone

    
27.10.2015 / 17:26
1

I do not know what you have to do with this, but leaving it in a way that opens several possibilities 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];
}
    
27.10.2015 / 18:06