Jump to content

[SOLVED] getting the value in between 2 characters


ldb358

Recommended Posts

i've been looking for a way to do this but cant seem to find anything:

what i want to do is say i have {test} i want to be able to replace it with 'hello' but if i have {test(1)} i want to replace test with the first value in an array(i already have a function that reads and replaces all of {test} but i cant get the array/number part of it to work).

Link to comment
Share on other sites

okay what im doing is a template parsing system where it reads the file and would replace {test} with content but lets say i have a place where it lists out 20 videos  i want to be able to put {test(1)} and it would be replaced with the first video(which would be stored in an array) and {test(2)} to be replaced with the second video and so on

Link to comment
Share on other sites

Would this work?

 

<?php
$str = 'some text and here is {test(1)} and here is another {test(2)} and it continues..';

function ownReplace($matches)
{
$replaces = array('first', 'second', 'third');
return $replaces[$matches[1]];
}

$str = preg_replace_callback('/\{.*?\(([0-9]+)\)\}/', 'ownReplace', $str); // Replace with number from array by index.
var_dump($str);

Link to comment
Share on other sites

Yes but you can try it can't you? The piece I posted replaces the {test(1)} with the value in $replaces[1] and {test(2)} with the value $replaces[2]. This will replace the values based on the array index. So you have to just make sure that your first video has then index 1 in the video array. Also if using with templates they are probably multi line data so you might wanna add 's' -modifier in the end of the regular expression so it will ignore new lines.

 

<?php
$str = preg_replace_callback('/\{.*?\(([0-9]+)\)\}/s', 'ownReplace', $str); // Replace with number from array by index.

Link to comment
Share on other sites

r u interested in using regex.. or you want to do this solely in php (well you know what I mean..)

 

regex would be easier but with plain old php you'd do

 

<?php
$data = "hello {abc(0)}";
$abc = array("world");
$start = 0;
while ($start = strpos($data,"{",$start)) {
	$end = strpos($data,"}",++$start);
	$in = substr($data,$start,$end - $start);
	if ($ns = strpos($in,"(")) {
		$var = substr($in,0,$ns);
		$index = substr($in,++$ns,strpos($in,")") - $ns);
		$repval = ${$var}[$index];
	} else {
		$repval = ${$in};
	}
	$data = str_replace('{'.$in.'}',$repval,$data);
	$start = strlen($repval) + $start;
}
echo $data;
?>

Link to comment
Share on other sites

it works perfectly now that i took a second look but how would i do it if i say had like {test(1)} but also had {hello(1)} and i didn't want hello switched(at least with that function)

thanks for the help

 

So you want only tags with certain name to be replaced?

 

<?php
$str = 'some text and here is {test(1)} and here is another {test(2)} and it continues.. {hello(1)} ... {somemore(10)}';

// And change the regexp to this.. will match all tags having 'test'
$str = preg_replace_callback('/\{test\(([0-9]+)\)\}/', 'ownReplace', $str); // Replace with number from array by index.

 

You can probably easily spot the word test in the regular expression. Just change that whatever you wish.

Link to comment
Share on other sites

r u interested in using regex.. or you want to do this solely in php (well you know what I mean..)

 

regex would be easier but with plain old php you'd do

 

<?php
$data = "hello {abc(0)}";
$abc = array("world");
$start = 0;
while ($start = strpos($data,"{",$start)) {
	$end = strpos($data,"}",++$start);
	$in = substr($data,$start,$end - $start);
	if ($ns = strpos($in,"(")) {
		$var = substr($in,0,$ns);
		$index = substr($in,++$ns,strpos($in,")") - $ns);
		$repval = ${$var}[$index];
	} else {
		$repval = ${$in};
	}
	$data = str_replace('{'.$in.'}',$repval,$data);
	$start = strlen($repval) + $start;
}
echo $data;
?>

 

This gives me warning with your test data and when I tested adding more tags with different names this will produce even more notices (error reportin E_ALL). Not that this will no prevent the script from running because they are not real errors but it is always good to program it that way there won't be any warnings or notices.

Link to comment
Share on other sites

okay.. its a warning which means the loop is gonna terminate because the offset no-longer exists..

 

<?php
error_reporting(E_ALL);
$data = "hello {abc(0)}, {ab} and like it!";
$abc = array("worl{d}");
$ab = "eat the banana!!";
$start = 0;
while ($start = @strpos($data,"{",$start)) {
	$end = strpos($data,"}",++$start);
	$in = substr($data,$start,$end - $start);
	if ($ns = strpos($in,"(")) {
		$var = substr($in,0,$ns);
		$index = substr($in,++$ns,strpos($in,")") - $ns);
		$repval = ${$var}[$index];
	} else {
		$repval = ${$in};
	}
	$data = str_replace('{'.$in.'}',$repval,$data);
	$start = strlen($repval) + $start;
}
echo $data;
?>

 

just throw in the @ to suppress warnings and you're all set :)

Link to comment
Share on other sites

ive gotten it down to this to search and replace through the document:

           
foreach($this->filter as $name => $value){
      $this->output = preg_replace('/{$name.*?([0-19]).*?}/', $value, $this->template);
}

but it doesn't actually replace any thing Ive never worked with regex so i dont exactly know what I'm doing but any help would be nice

Link to comment
Share on other sites

on yours then i can't get to work exactly how i need it, all my tags are passed to the function via array these are the only ones i want changed and the values are passed in the same array for example $array['test'] = 'hello' and i need it to replace the tag(key) with the value for it

Link to comment
Share on other sites

mine replaces {abc(0)}

with $abc[0]

 

and {wfg} with $wfg.. whatever variables you have in the string have those variables in php variables, and it'll work just fine. theres no reason this post should have 14 posts (considering mine) and regex will not give you any advantage over plain old php, more than likely will just slow your script down as it goes back and forward and never just left to right

Link to comment
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.