Jump to content

how to extract (very simple), II approach


AndyPSV

Recommended Posts

all the same: http://www.phpfreaks.com/forums/index.php?topic=349404.0

 

<?php
$variable = 'answer_comment';
preg_match("/(?<=$variable)[^A-Za-z]+/sm", $subject, $match);

(with code that worked)

 

 

but instead of only numbers, I've got now:

 

(code above only selects to first appearance of: "#", then it stops)

123321#c31221

12#c15

21321#c2153

 

 

How to extract such data with the usage of this modified preg_match?

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/252687-how-to-extract-very-simple-ii-approach/
Share on other sites

Hmm, not entirely sure on what you need now :shrug:

 

However, something like this might work. Again, untested:

<?php
$variable = 'answer_comment';
preg_match("/(?<=$variable)(\d+#[a-zA-Z]\d+(:\d+)?)+?(?![a-zA-Z]{2})/sm", $subject, $match);

 

That will look for:

- Something that begins with the variable.

- Is constantly looking ahead to make sure that: Two letters don't match (indicates a word)

- The following will happen between one and infinity times

  - Starts with a digit.

      - There is at least one and infinity digits.

  - Followed by a hash symbol

  - Followed by one letter, upper or lower case

  - Followed by between one and infinity digits

  - Between one and zero times the following will happen

    - Followed by a colon

    - Followed by between one and infinity digits

 

 

If you have another variation of a regex question, before asking it, please go to http://www.regular-expressions.info/tutorial.html and read up on regex. If, if, you still cannot understand how to get what you need, then ask the question.

 

Hope this helps,

Joe

It doesn't wants to work.

 

<?php

$CID = 'question_comment';
$s = 'answer_comment
21221#c321321321
213231321213
21332132121121212112
122121434
12

question_comment
1#c8
5093320#c21
3215533

';

preg_match("/(?<=$CID)(\d+#[a-zA-Z]\d+(:\d+)?)+?(?![a-zA-Z]{2})/sm",$s,$arr); /*$arr = trim($arr[0]); $arr = explode(NL,$arr);*/ var_export($arr); die;


?>

 

I think, I would use explode(), however: it's not so elegant as the preg_match().

 

 

--------------------------------- (didn't thought that before)

 

 

<?php

$CID = 'question_comment';
$s = 'answer_comment
21221#c321321321
213231321213
21332132121121212112
122121434
12

question_comment
1#c8
5093320#c21
3215533

';

$arr1 = explode($CID,$s);
$arr = explode("\r\n\r\n",$arr1[1]);

#preg_match("/(?<=$CID)(\d+#[a-zA-Z]\d+(:\d+)?)+?(?![a-zA-Z]{2})/sm",$s,$arr); /*$arr = trim($arr[0]); $arr = explode(NL,$arr);*/

var_export($arr); die;


?>

 

SOLVED, thank you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.