0

I need to get part of code inside < ins> tag from source:

<some tags there and code there><ins class="adsbygoogle"
     style="display:inline-block;width:670px;height:100px"
     data-ad-client="ca-pub-9438155779940585"
     data-ad-slot="1115596158"></ins>bla bla there <tags></tags>

I googled and searched all stackoverflow for hours, use site https://regexr.com/ and cant do it!

I tried :

/<ins[.\s]*<\/ins>/ism

/<ins[.|\s]*<\/ins>/ism

/<ins(\w\d\s.)*<\/ins>/ism (i know thats is totally wrong, but i also tried many combos like this)

And all million other combinations. Nothing helps, please help!

wtfowned
  • 124
  • 1
  • 11
  • Do you mean the content between ``, all the attributes of the `` element, or the "bla bla there" part? – jeromegamez Nov 22 '17 at 17:59
  • 4
    regex is not the tool you want to parse html. Other similar questions have been asked before, notably: https://stackoverflow.com/a/1732454/1836940 – Daniel Causebrook Nov 22 '17 at 18:00
  • Thanks for everyone, I understand, parsing HTML with regexp - bad idea. Thats why now I use https://github.com/Imangazaliev/DiDOM and its a great and fast solution for me! Also it isn't easy to make some changes, for example its not possible to delete all comment nodes without braking brain. – wtfowned Nov 22 '17 at 20:45

2 Answers2

0

To match everything within the ins tags you want to use:

<\s*ins[^>]*>([^<]*)<\s*\/\s*ins\s*>

Or if the tag will be <ins> always with no id='' etc etc .. You can simply:

<ins>([^<]*)<\/ins>

This for example

<ins>
   This is
   A Match
</ins>

Would return

This is
A Match

Example on REGEX 101 with detailed explanation for matching <ins id="whatever"></ins>

Example on REGEX 101 with detailed explanation for matching just <ins></ins>

Zak
  • 6,976
  • 2
  • 26
  • 48
0

I was not sure which part you actually want to retrieve, so here is a selfsufficient test script that includes all I could imagine :).

<?php

$src = <<<SRC
<some tags there and code there><ins class="adsbygoogle"
     style="display:inline-block;width:670px;height:100px"
     data-ad-client="ca-pub-9438155779940585"
     data-ad-slot="1115596158">ins_content</ins>in_between<tags></tags></some>
SRC;

// What's inside <ins> and </ins>
$pattern = '@<ins[^>]*>(.*)</ins>@ium';
preg_match($pattern, $src, $matches);
echo $matches[1].PHP_EOL; // ins_content

// All attribues of the <ins> element
$pattern = '@(<ins([^>]+))@ium';
preg_match($pattern, $src, $matches);
echo $matches[2].PHP_EOL; // class="adsbygoogle" style...

// Everything between </ins> and <tags>
$pattern = '@ins>(.*)<tags@ium';
preg_match($pattern, $src, $matches);
echo $matches[1].PHP_EOL; // in_between
jeromegamez
  • 3,348
  • 1
  • 23
  • 36