Jump to content

Recommended Posts

Hi

 

Lets say i have the following string

 

Before{block}After

 

and the following preg_match

 

$content = "Before{block}After";

preg_match ( '%([A-Za-z0-9]*){(.*)}([A-Za-z0-9]*)%', stripslashes ( $content ), $data );

 

Works perfect, picks out $data[1] = "Before"  $data[2] = "block" $data[3] = "After"

 

But, say i have this

 

Before{block}After{block}

 

it doesnt work, it doesnt seem to pick up that i need to have $data[1] = "Before" $data[2] = "block" $data[3] = "After" $data[4] = "block"

 

Any help would be great

Link to comment
https://forums.phpfreaks.com/topic/176951-regex-preg_match/
Share on other sites

Depending what exactly you need, there might well be several different "answers" to your problem.  Do you literally just want to amend the regular expression to accept that second {block}, or any number of {block}s with before/after text, or ... ?

Link to comment
https://forums.phpfreaks.com/topic/176951-regex-preg_match/#findComment-932983
Share on other sites

hi

 

i am wanting to grab everything outside the {block} and the {block} itself.

 

so for example the actual thing may look like this

 

Some text {block identifier='test'} some more text {block identifier='another_test'}

 

and so on. My code is basically used so i can take the {block} tags and read the identifier and load up a plugin in my php based on what the identifier is set to, perform the code operation and replace the whole {block} tag...

 

But for now, im trying to get it to identify any number of block tags

 

 

Link to comment
https://forums.phpfreaks.com/topic/176951-regex-preg_match/#findComment-932991
Share on other sites

<?php

$content = "Before{block}After{blockb}Now{Blockc}A";

preg_match_all('~([a-zA-Z0-9]+)(?:{(.+?)})?~', stripslashes ($content), $tdata );

$data = array();

foreach($tdata[2] as $k=>$v)
{
$data[] = $tdata[1][$k];
if($v != "")
	{
	$data[] = $v;
	}
}

print_r($data);

?>

Link to comment
https://forums.phpfreaks.com/topic/176951-regex-preg_match/#findComment-933007
Share on other sites

if your goal is to figure out what identifiers are used... this will return an array of identifiers stored in $matches[1]...

 

<?
$str = "before text{block identifier='test'}middle text{block identifier='test2'}end text";
preg_match_all("/{block identifier='([^']+)'}/",$str,$matches);
print_r($matches[1]);
?>

Link to comment
https://forums.phpfreaks.com/topic/176951-regex-preg_match/#findComment-933022
Share on other sites

fooDigi

 

Even with all the years programming i have done, i still have a big issue with getting to grips with regex. Could you possibly break down your example and tell me what its doing. the [^']  part i understand, butut what is the + for?

Link to comment
https://forums.phpfreaks.com/topic/176951-regex-preg_match/#findComment-933033
Share on other sites

also, since you will want to replace the entire {block}, $matches[0] will contain those matched strings... so from there you will be able to replace those with the output of your plugins...

 

forgive me, but i went a bit further with my code, and i might as well post it... ignore it, if it is a total miss on what you need...

 

<?

function loadPlugin($id)
{
switch($id)
{
	case 'test':
	return '[plugin test loaded]';
	break;
	case 'test2':
	return '[plugin test2 loaded]';
	break;
	case 'test3':
	return '[plugin test3 loaded]';
	break;
	case 'test4':
	return '[plugin test4 loaded]';
	break;
	default:
	return '[invalid plugin]';
}
}

$str = "before text{block identifier='test'}middle text{block identifier='test2'}end {block identifier='test3'}text{block identifier='test4'}sdfasd fasdf asdf";
preg_match_all("/{block identifier='([^']+)'}/",$str,$matches);

for($i=0;$i<count($matches[0]);$i++)
{
$text = loadPlugin($matches[1][$i]);
$str = str_replace($matches[0][$i],$text,$str);
}

echo $str;
?>

Link to comment
https://forums.phpfreaks.com/topic/176951-regex-preg_match/#findComment-933048
Share on other sites

Okay, + is the same as * except * will match it 0 times. So say you have {} then {(.*)} will match it, but + matches once or more so {(.+)} will not, because there has to be at least one thing there.

 

Recap:

 

* Matches 0 or more

+ Matches 1 or more

Link to comment
https://forums.phpfreaks.com/topic/176951-regex-preg_match/#findComment-933098
Share on other sites

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.