2

I need to replace some words or some sentences and convert it to underline. I'm using PHP and find one reference: PHP Regex, extract all custom tags from text

Somehow, the case only covers for single words but not sentences. How to make the regex also can catch everything enclosed by ## tag?

Let say my input will be such as:

"ll the Lorem Ipsum ##generators## on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over ##200 Latin words##, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always ##free from repetition##, injected humour, or non-characteristic words etc."

Then the output will be:

"ll the Lorem Ipsum ____1____ on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over ____2____, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always ____3____, injected humour, or non-characteristic words etc."

Can anyone help me on how to get the regex pattern?

shA.t
  • 16,580
  • 5
  • 54
  • 111
Nere
  • 4,097
  • 5
  • 31
  • 71

2 Answers2

2

I think the regex is:

/##[^#]+##/g

[Regex Demo]


$text = 'll the Lorem Ipsum ##generators## on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over ##200 Latin words##, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always ##free from repetition##, injected humour, or non-characteristic words etc.';

preg_match_all('/##[^#]+##/', $text, $matches, PREG_SET_ORDER);

for ($i = 0; $i < count($matches); $i++) {
  $text = preg_replace("/".$matches[$i][0]."/", "___".strval($i+1)."___" , $text, 1);
}

[PHP Demo]

shA.t
  • 16,580
  • 5
  • 54
  • 111
  • Hi, just one more how to make it replace with counting number? for example... _____1_____,__ ___2_____, etc?I'am using `preg_replace` anyway. – Nere Aug 15 '17 at 08:26
  • You need to use `for` or `foreach` over results of `preg_match_all` - please check this [question](https://stackoverflow.com/q/8127288/4519059) ;). – shA.t Aug 15 '17 at 08:32
  • Or you can use `for` with `preg_replace($search, $replace, $subject, 1);` that replace just first match ;). – shA.t Aug 15 '17 at 08:38
  • In this line, `$text = preg_replace("/".$matches[$i][0]."/", "___".strval($i+1)."___" , $text, 1);` ...Errors `unknown modifier ...` since I use input `##4/5##`. Then how to solve it ? ;) – Nere Aug 15 '17 at 23:19
  • I can't understand your situation well, can you please ask it in another question? ;). – shA.t Aug 16 '17 at 04:15
  • @AbuHassan for your error: you need to use `preg_quote($matches[$i][0], "/")` instead of `$matches[$i][0]` else the slash in "4/5" will end the pattern. – bobble bubble Aug 16 '17 at 13:13
2

Another idea using @shA.t's regex would be the use of preg_replace_callback with a function that increments a variable. So it can be done without loop, which might improve efficiency a bit.

$str = preg_replace_callback('/##[^#]+##/', function($m) use (&$i) {
  return "____". ++$i ."____";
}, $str);

See php demo at eval.in

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • How to inject counter to your function? I need to use array to count..since I need to integrate to next code. – Nere Aug 15 '17 at 23:26
  • 1
    @AbuHassan This is a premium answer. What exactly do you need that this answer doesn't provide? Do you need `$i` outside of the function scope? -- it's there. – mickmackusa Aug 16 '17 at 04:33
  • Yes exactly. I need to reset the counter. – Nere Aug 16 '17 at 04:41
  • @AbuHassan not sure if I understand, can't you just use `$i = 0;` to reset? – bobble bubble Aug 16 '17 at 08:56
  • @bobblebubble for my case, `$i` value shows like `_____3_____`,`_____8_____` not start from zero. – Nere Aug 16 '17 at 09:14
  • 1
    @AbuHassan: How do you use it? Put `$i = 0;` just before the preg_replace_callback. – Toto Aug 16 '17 at 09:40