S
S
SeiLove2015-10-27 12:15:22
PHP
SeiLove, 2015-10-27 12:15:22

Regular expression error?

I want to split the line - so that the yurl is separate, the name is separate, the description is separate.

$content =  '<img src="/image/fluffybunny.jpg" title="Тайтл" alt="Деск" />';
$image   =  preg_match_all('<img.*?(?:(?:\s+(src)="([^"]+)")|(?:\s+(title)="([^"]+)")|(?:\s+(alt)="([^"]+)")|(?:\s+[^\s]+))+.*/>', $content, $matches);

for ($i=0; $i< count($matches[0]); $i++) {
  echo "Адрес страницы: " . $matches[0][$i] . "\n";
  echo "Название: " . $matches[1][$i] . "\n";
  echo "Описание: " . $matches[3][$i] . "\n";
}

For some reason, the line is parsed incorrectly and outputs:
" Page address: img src="/image/fluffybunny.jpg" title="Title" alt="Desk" / Title: src Description: title "
Maybe someone will see what's wrong ?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Alexey Ostin, 2015-10-27
@nitso

stackoverflow.com/questions/1732348/regex-match-op...
Use any available xml parser like simple_html_dom . With it, your design will be simple and readable:

foreach($html->find('img') as $img) {
      echo "Адрес страницы: " . $img->src . "\n";
      echo "Название: " . $img->title . "\n";
      echo "Описание: " . $img->alt . "\n";
}

It would be more correct to write the variant with a regular expression in the form of obtaining key-value pairs and further processing the received data: https://regex101.com/r/aS7qG0/1 . No one guarantees you both the presence and the order of the attributes in the tag.

O
Optimus, 2015-10-27
Pyan @marrk2

I am sincerely surprised by people who shove everything into one regular season. Parse <img(.*?)> separately and quietly extract src=\"(.*?)\" title=\"(.*?)\" from it

M
Max, 2015-10-27
@MaxDukov

the last / was not escaped.
necessary \/

<img.*?(?:(?:\s+(src)="([^"]+)")|(?:\s+(title)="([^"]+)")|(?:\s+(alt)="([^"]+)")|(?:\s+[^\s]+))+.*\/>

D
Dmitry, 2015-10-27
@mytmid

Maybe like this?

$content =  '<img src="/image/fluffybunny.jpg" title="Тайтл" alt="Деск" />';

function parse( $m )
{
  print_r($m); // проверка
}

preg_replace_callback( '!<img.*src="([^"]+)".*title="([^"]+)".*alt="([^"]+)".*\>!us' , 'parse', $content );

R
Ravshan Abdulaev, 2015-10-27
@ravshanium

your regex would be good to use like this:

for($i=1; $i< count($matches)-1; $i++){
   $var = $matches[$i][0];
 $$var = $matches[$i+1][0];
}

var_dump($src, $title, $alt);

Given that the order of the attributes can be reversed - this will work...
codepad.viper-7.com/douGqC

D
Denis, 2015-10-27
@prototype_denis

When will programmers realize that it is too desirable to work with xml, html and other heresy like a tree, and not try to parse them with regular expressions, which are even unrealistic to understand the first time and which break if you add some insignificant nonsense to a valid tree structure .
Sorry, got...

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('xdebug.var_display_max_depth', -1);

$string = '<img src="/image/fluffybunny.jpg" title="Тайтл" alt="Деск" />';

$array = (array) simplexml_load_string($string);
$attributes = $array['@attributes'];

var_dump($attributes);

// array(3) {
//   'src' =>
//   string(22) "/image/fluffybunny.jpg"
//   'title' =>
//   string(10) "Тайтл"
//   'alt' =>
//   string(8) "Деск"
// }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question