Select all elements of an array in a variable with Regex in PHP

Asked

Viewed 333 times

2

I would like to do a regex to apply a class to each row with Roman numbers, the regex would be? /(<li>\s*<p>$romanos\)_\-_/ ?

The documents are like this:

<li>
  <p>I - blablabla</p>
</li>
<li>
  <p>II - blablabla</p>
</li>
<li>
  <p>III - blablabla</p>
</li>

I’d like to keep it that way

<li class="inciso">
  <p>I - blablabla</p>
</li>
<li class="inciso">
  <p>II - blablabla</p>
</li>
<li class="inciso">
  <p>III - blablabla</p>
</li>

I created an array of Roman numbers

$romanos =  array(I,II,III,IV,V,VI,VII,VIII,IX,X,XI,XII,XIII,XIV,XV,XVI,XVII,XVIII,XIX,XX,XXI,XXII,XXIII,XXIV,XXV,XXVI,XXVII,XXVIII,XXIX,XXX,XXXI,XXXII,XXXIII,XXXIV,XXXV,XXXVI,XXXVII,XXXVIII,XXXIX);

Here are the text variables I’m looking for

$semClassInciso = '/   <li>
  <p>$romanos - /';

Here is the text variable with the LI tag whose class is "inciso

$comClassInciso = '/   <li class="inciso">
  <p>$romanos - /';

Here is the document where the text with the initials

$documento = file_get_contents($arquivo);

$documento1 = preg_replace($semClassInciso, $comClassInciso , $documento);

echo  $documento1;

In short, I want to add the tag <li class="inciso"> in all occurrences of Roman numerals at the beginning of a paragraph.

  • I couldn’t understand, you can explain better what you want?

  • In the variable $semClassInciso, what varies is the number, I have section 1, 2, 3, 4 .... and I would like to add the initial class in all occurrences

  • But the numbers are in Roman

  • Section I, II, III

  • The title doesn’t match what you want, but from what I understand, try something like this: http://ideone.com/zUZRCL

  • I edited the question, see if it was clearer, a little, but still do not know how to make more clear the title

  • See if this is something you want to do: http://ideone.com/7eDrPr

  • 1

    the explanation was confused to understand. I edited an excerpt and added a summary that explains what you want.

  • @qmechanik, put which lines I have in the file, this is the structure, the script should read each line and apply the class inciso in each line

  • @Try it this way: http://ideone.com/0gZclc, see if it works in your case.

Show 5 more comments

1 answer

1


Try to use the preg_replace_callback something like this:

<?php

function adicionaTexto($matches) {
    return '<li class="inciso">' . $matches[0];
}

$documento = file_get_contents($arquivo);

$documento1 = preg_replace_callback($semClassInciso, adicionaTexto, $documento);

echo  $documento1;

Good I’ll leave the tip ai of callback can be useful. You can also try something like this:

$re = "/(<li>)(\s*<p>[MDCLXVI]+)/"; 
$str = "<li>
  <p>I - blablabla</p>
</li>
<li>
  <p>II - blablabla</p>
</li>
<li>
  <p>III - blablabla</p>
</li>";


$subst = "<li class=\"inciso\">$2"; 

$result = preg_replace($re, $subst, $str);

An example here -> https://regex101.com/r/dQ5oD0/2

  • I edited the question, I am not being and clear, and I do not know how to explain, kkk, see if it became a little clearer the question

Browser other questions tagged

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