-3

I would like to insert a class with regex and preg_replace

echo preg_replace("/<li\>\s*<p\>[a-z]\)\s/", "/<li class=\"inciso\"\>\s*<p\>[a-z]\)\s/", $documento);

This is the model of text that I haveEste é o modelo das linhas do meu documento:

<li>
  <p>a) long text</p>
</li>
<li>
  <p>b) long text</p>
</li>
<li>
  <p>c) long text</p>
</li>

New example, let´s say that is not a HTML, is just a simple list, and you wanna a change from this:

a) long text
b) long text
c) long text

To this:

a) new text long text
b) new text long text
c) new text long text

echo preg_replace("/[a-z]\)\s/", "/[a-z]\)\snew\stext/", $documento);

Is this correct?

Alê Moraes
  • 175
  • 9
  • 4
    [Don't parse HTML with regex!](http://stackoverflow.com/a/1732454/418066) – Biffen May 05 '15 at 13:39
  • It depends. If you don't have to **parse** it, it's fine and maybe easier. It depends on the task and on the document you have. – ColOfAbRiX May 05 '15 at 13:44
  • I edit the question, at the end to make it clear – Alê Moraes May 05 '15 at 13:49
  • Yes, you can do it. Few questions about the part you added: you are replacing "every word followed by a space. Is that what you want? Or do you need to replace just the word "long"? – ColOfAbRiX May 05 '15 at 13:52
  • Yes, it has a space, and the word "long", is just a sample, the content changes from this part. The only part that stay stable is the beginning to the space after the ")" – Alê Moraes May 05 '15 at 13:55

2 Answers2

1

IF, and I emphasize again IF, the input text you have is like the one you posted here, then you can assume you can find a safe pattern to replce, as you won't see this pattern somewhere else:

preg_replace("/<li>/", "<li class=\"inciso\"\/>", $documento);

This will replace every occurrence of <li> with the modified version. If there are <li> that you won't replace then it becomes more difficult and you should use a DOM or SAX parser

UPDATE after your update: You can match a word and add something before it with:

preg_replace("(long)", "new text $1", $documento);

Have a look at backreferences

ColOfAbRiX
  • 1,039
  • 1
  • 13
  • 27
0

use str_replace instead.

$find    = '<li>';
$replace = '<li class="inciso">';


echo str_replace($documento, $find, $replace);
Lisa
  • 54
  • 1
  • 5
  • than describe better what you want. (the exact strings). In general you should not use regexp for that. – Lisa May 05 '15 at 13:49
  • Sorry Lisa. ;) . But if I can´t use regex, what are my option? – Alê Moraes May 05 '15 at 13:57
  • If you have a way to uniquely identify in one go all the `
  • ` you need (like if they are always followed by a character, or if they have specific attributes and so on...) to replace then you can use regexes, otherwise no
  • – ColOfAbRiX May 05 '15 at 13:57
  • @AlêMoraes try using a html parser , http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php ( see part about Simple HTML Dom Parser ). regexp gets too complicated and you'll run into issues where your string does not match ( for example characters like ẽ) – Lisa May 05 '15 at 14:04