HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Regex-ing an array

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arrayregexing

Problem

I am new using regex expression. After much digging I came up with this code. I would like to know if this is the correct way. I want to search in Joomla for some html tags with specific class and then replace the code between those tags. I want to use an array ($patterns) with tag and classes and another array ($replacements) with replacements values.

$body = JResponse::getBody();
$patterns = array
(
array('a','logo')
array('span','main')
);

$replacements = array
(
array('some text')
array('another text')
);

foreach ($patterns as $key => $value) {
$body = preg_replace('/()(.*?)()/', '$1'.$replacements[$key][0].'$3', $body); 
    }

Solution

This is never going to work

HTML uses tags that are nested. So if you are looking for pattern array('ul','nicelist') in the following bit, it will fail:


  item 1
  item 2
    
      subitem 1
      subitem 2
      subitem 3
    
  
  item 3
  item 4


You can see it will hit the wrong `. So your solution is not general enough.

The only good way to manipulate HTML in PHP is to use code that understands the DOM. See:

http://php.net/manual/en/domdocument.loadhtml.php

or:

http://sourceforge.net/projects/simplehtmldom/files

These were made to do the things you try to do with
preg_replace()`.

As for your code itself: I am baffled by the seperation between patterns and the replacements. Why not make one array like:

$replacements = [['tag' => 'a',    'class' => 'logo', 'insert' => 'some text'],
                 ['tag' => 'span', 'class' => 'main', 'insert' => 'another text']];


Keep things together that belong together. I've also used the new array syntax, you need PHP 5.4 or higher, I think, and added keys. The keys make your code better readable, especially if you revisit the code after some time.

I will not go into the reasons why it's a bad idea to do what you're trying to do, replacings pieces of HTML like this, even if you were to use the DOM. I guess there can be some situations in which this is needed?

Code Snippets

<ul class="nicelist">
  <li>item 1</li>
  <li>item 2
    <ul class="nicesublist">
      <li>subitem 1</li>
      <li>subitem 2</li>
      <li>subitem 3</li>
    </ul>
  </li>
  <li>item 3</li>
  <li>item 4</li>
</ul>
$replacements = [['tag' => 'a',    'class' => 'logo', 'insert' => '<p>some text</p>'],
                 ['tag' => 'span', 'class' => 'main', 'insert' => '<p>another text</p>']];

Context

StackExchange Code Review Q#63786, answer score: 2

Revisions (0)

No revisions yet.