Php 7.1, How to make a REGEX of the following standard:<div class='inline-render' data-type='credit-card' data-Listing-id='1234'>?

Asked

Viewed 39 times

0

I have a text where the following pattern can repeat many or no times and with different values of data-type:

<div class='inline-render' data-type='ALGUM TIPO' data-listing-id='VALOR'>

I need to retrieve all the VALUES of id when the guy is credit-card.

Example: In the text below I have two occurrences of 'credit-card' which are the following:

<div class='inline-render' data-type='credit-card' data-listing-id='1234'>
e
<div class='inline-render' data-type='credit-card' data-listing-id='1111'>

In the example below I would have to repuperate 1234 and 1111.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lobortis purus sapien, at vulputate magna condimentum a. Phasellus massa sem, ullamcorper ut pellentesque eu, pulvinar sit amet risus. Suspendisse placerat odio sapien, ac efficitur purus congue a. Nunc nunc metus, convallis <div class='inline-render' data-type='credit-card' data-listing-id='1234'> sed leo at, fringilla lacinia nibh. Nullam imperdiet facilisis efficitur. Maecenas venenatis nunc sit amet nibh facilisis euismod. Vivamus sed <div class='inline-render' data-type='credit-card' data-listing-id='1111'>lacinia odio. Nunc et purus sit amet felis tempus fermentum finibus tempor mi. Phasellus at nibh nec mi laoreet lobortis. Quisque eget dui quis purus fermentum mollis.

Sed mauris est, congue eget tortor sed, efficitur cursus augue. Curabitur rutrum erat diam, non egestas leo accumsan sit amet. Nunc varius ante metus, <div class='inline-render' data-type='nota' data-listing-id='1111'>congue luctus sapien ullamcorper nec. Nullam eu hendrerit odio, placerat ultrices libero. Proin fringilla orci et magna eleifend, eget tincidunt ex ultricies. Quisque <div class='inline-render' data-type='roupa' data-listing-id='1234'>eget imperdiet mauris. Cras purus felis, aliquet quis vulputate vitae, aliquet nec lectus. Cras at purus eget ex posuere faucibus. Duis rhoncus feugiat pellentesque. Pellentesque vitae felis et metus convallis sagittis eu in arcu.

UPDATE

I made the following solution, since the name of the data-type has to be in REGEX:

preg_match_all ("/data-type='credit-card' data-listing-id='(.*?)'/U", $data['html'], $pat_array);
dd($pat_array);
array:2 [
  0 => array:3 [
    0 => "data-type='credit-card' data-listing-id='1234'"
    1 => "data-type='credit-card' data-listing-id='1111'"
  ]
  1 => array:3 [
    0 => "1234"
    1 => "1111"
  ]
]

I’m just not satisfied with the white space between -card' dat as it may vary and REGEX does not recover.

  • No time to write an answer now, but maybe this will help you: https://answall.com/q/373962/112052

  • It helps a lot. But it’s not just indenting attributes, it has a condition. It can only be when the data-type is credit-card. So I think the word creditcar has to enter the regex. I’ll update what I’ve got so far.

  • In the answer there, there is a code that uses regex, and you can get the attributes and respective values. If I have time I’ll write an answer, but at first I think I can adapt that code

1 answer

1


Browser other questions tagged

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