Jump to content

string extraction


xploita

Recommended Posts

say that i have  a template file like this

 

text

<template start>

text

text <replacable1> text <replaceble2> text text text

text

</template end>

text

 

what can i do to extract the bold text between the start and the end of the template tags(to replace the replacable tags with the variable value?

 

Link to comment
https://forums.phpfreaks.com/topic/47587-string-extraction/
Share on other sites

preg_match('/<template start>(.*?)<\/template end>/i',$text,$match);
str_replace(array('<replacable1>','<replacable2>'),array('replacement1','replacement2'),$match[1]);
echo $match[1];

// ...or...

$string = preg_replace('/(<template start>.*?)<replaceable1>(.*?)<replaceable2>(.*?<\/template end>)/i',
'$1'.'replacement1'.'$2'.'replacement2'.'$3',$string);

Link to comment
https://forums.phpfreaks.com/topic/47587-string-extraction/#findComment-232331
Share on other sites

Yeah, sorry.  The preg_*s needed the 's' modifier to make them match new lines, and the str_replace needed to assign to something.

 

<pre><?php

$string = "text\n<template start>\ntext\ntext <replaceable1> text <replaceable2> text text text\ntext\n</template end>\ntext";

echo "----- str_replace ------\n";
preg_match('/<template start>(.*?)<\/template end>/si',$string,$match);
echo str_replace(array('<replaceable1>','<replaceable2>'),array('replacement1','replacement2'),$match[1]);

echo "----- preg_replace ------\n";
// ...or...

echo preg_replace('/(<template start>.*?)<replaceable1>(.*?)<replaceable2>(.*?<\/template end>)/si',
'$1replacement1$2replacement2$3',$string);

?>

</pre>

 

Yields the following:

 

<pre>
----- str_replace ------
text
text replacement1 text replacement2 text text text
text
----- preg_replace ------
text
<template start>
text
text replacement1 text replacement2 text text text
text
</template end>
text
</pre>

Link to comment
https://forums.phpfreaks.com/topic/47587-string-extraction/#findComment-232747
Share on other sites

There are a number of ways to do that.  The one that I would probably use involves either preg_replace with the /e modifier or preg_replace_callback.  You create a function that does the "inner processing," and use preg_replace to locate the big template block and replace it with the reprocessed output.

 

For example, something like (untested):


$string = "text <template> text text<tr><td>cell1</td><td>cell2</td></tr>text</template> text";

$new_string = preg_replace(/'<template>(.*?)</template>/ies','_your_callback(\'$1\')',$string);

// $1 should contain everything between the template tags.
// $new_string will then contain the old string with no template tags and everything in between changed.

function _your_callback($inner_block) {
return str_replace(
array(
	'\"',   // You'll need something like this due to PHP auto-escaping behavior.
	'<td>',
	'</td>',
	'<tr>',
	'</tr>'
),array(
	'"',
	'whatever1',
	'whatever2',
	'whatever3',
	'whatever4'
),$inner_block);
}

 

preg_replace_callback() is similar.

Link to comment
https://forums.phpfreaks.com/topic/47587-string-extraction/#findComment-233207
Share on other sites

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.