Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
PHP replace string starting with xxx and ending with yyy after 5 characters

Input:

$str = 'hi test1="12c4 ab3d" blablabla test1="5678 sdfg"'

So I need to remove string between 'test1="' and '"' after 5 characters, like that:

'hi test1="12c4" blablabla test1="5678"'

I already tried:

$str= preg_replace('/test1="[\s\S]+?"/', 'test1=""', $str);

But my ouput was:

'hi test1="" blablabla test1=""'
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Alternatively, without a regex and using simple strpos() and substr()

$str = 'hi test1="12c4 ab3d" blablabla test1="5678 sdfg"';

$find = 'test1=';
$p1 = stripos($str, $find) + strlen($find)+5;
$p2 = strripos($str, $find) + strlen($find)+5;

$new =  substr($str, 0, $p1) . substr($str, $p1+5, $p2-$p1-5) . substr($str, $p2+5);
echo $new . PHP_EOL;
over 4 years ago · Santiago Trujillo Relatório

0

You can use

$str = 'hi test1="12c4 ab3d" blablabla test1="abcd 3456 sdfs 2435"';
echo preg_replace('~\btest1="[^"\s]*\K[^"]+~', '', $str);
// => hi test1="12c4" blablabla test1="abcd"

See the PHP demo and the regex demo.

Details:

  • \b - a word boundary
  • test1=" - a literal text
  • [^"\s]* - zero or more chars other than whitespace and "
  • \K - match reset operator that removes the text matched so far from the match memory buffer
  • [^"]+ - one or more chars other than ".
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda