Jump to content

Recommended Posts

Hi,

 

(this is a PCRE related issue)

 

This has probably been asked already, but i did a search and couldnt find my anwser, so here goes:

 

Im trying to preg_match and then preg_replace between two tags.

 

The html would look like this:

 

<dmod::user|overview>
<drow>asdasdasd</drow>
</dmod>

 

For the preg_match i tried this but it failed and im pretty clueless on how to get it to work:

 

if(preg_match('/(<dmod:(.+?)(>)(.+?)(</dmod>)/iU', $str)) {
    echo 'match?';
}

 

If u guys got any tips on how to fix this, or point me in the right direction id really appreciate it.

 

Thanks,

Dlate

Link to comment
https://forums.phpfreaks.com/topic/119043-regex-get-text-from-within-two-tags/
Share on other sites

Update 2:

 

I solved it!

problem was i wasnt escaping the pattern proper so my modifiers would get messed up.

correct pattern was :

 

if(preg_match('/(<dmod:(.+?)(>)(.+?)(<\/dmod>)/iU', $str)) {
    echo 'match?';
}

 

Hopes this might help other people with similiar problems

Solved it!

 

I missed the /s for spaces, i really hope someone in the future can get help from this topic tho, took me a while to figure it out.

 

final code:

 

$pattern = '/<dmod:.+?)>(.+?)<\/dmod>/is';
if(preg_match($pattern, trim($output), $matches)) {
   $output = preg_replace($pattern, $matches[2], $output);
}

- By changing the delimiter, there's no need to escape /.

- Greediness is generally more efficient than laziness; use [^>]+.

- Why run a match and a replace?

 

<pre>
<?php
$output = <<<DATA
	<dmod::user|overview>
		<drow>asdasdasd</drow>
	</dmod>
DATA;
echo htmlspecialchars($output), '<hr>';
$pattern = '%<dmod:[^>]+)>(.+?)</dmod>%is';
$output = preg_replace($pattern, '$2', $output);
echo htmlspecialchars($output);
?> 
</pre>

Hehe nice, didnt know u could change the delimiter :P

As far as me using preg_match as well as replace is because i want to save the data between the tags and then replace it.

 

Thanks for ur input, i really appreciate any help i can get with regex as im a complete newbie with it,

could you please explain why ur putting <<<DATA in ur string?

preg_match only matches, but preg_replace matches and replaces (what it matches).

 

Essentially, what you have is...

  if (my string matches the pattern) {

    run the replace against the pattern

  }

...which is pointless because you cannot replace what you cannot match--all you need is the "run the replace against the pattern" step.

 

<<< is heredoc syntax.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.