0

ok so i know how to do this within theory but not how to code this one. i have within my page a {blah:6}. i know that if i want to call a static string i would do this

$replaced_content = str_replace("{blah}", $page_name, $page_content);

but the problem is that the number can be changed by the user to what ever number the user would like. the could choose {blah:3} or {blah:999}. So im not sure how to code this

my goal is to find the string then extract the number from it to use later, but still replace the string to what i need

example

{blah:3}

gets replaces to (3 times)

<li></li>
<li></li>
<li></li>

any help would be very appreciated. I have been searching for the answer on my own but i have come up dry

4
Contributors
20
Replies
21
Views
6 Years
Discussion Span
Last Post by nicksontinko
0

Hi,

A regular expression would be able to offer a suitable solution here. For example:

// This should find all numbers and store them in the array $matches 
preg_match_all('/^blah:(\d+)$/', $pageContent, $matches);
// This will replace all blah:number with the value of $pageName
preg_replace('/^blah:(\d+)&/', $pageName, $pageContent);

If you want to use the number of determine how many times the placeholders are replaced, you'll need to add an extra step in between the two defined above.

R.

0

first off thanks alot. but i was wondering if you could break that down for me. or if there is a tutorial somewhere that could teach me what all the (\d+)$/ stuff really does. i know what it does but i dont know how to write it on my own.

its just one of my things that when presented with me code i like to make sure i understand of it ticks before moving forward. hope that makes sense

0

ok so im having some problems and i even took a look at the manual but im still lost so i have

// This should find all numbers and store them in the array $matches 
preg_match_all('/^blah:(\d+)$/', $replaced_content, $matches);
foreach($matches as &$value){
	$numlimit = $value;
}
echo $numlimit;

all it echos out is array. with no values at all.

Is this supposed to just grab the grab and remove the number from that string.

is there a way to make it not be in an array. this placeholder will only show up once in the page

0

Hi,

You can find out more about regular expression syntax via Google, or the PHP.net website.

Can you provide a specific example of the input text which includes the placeholders you wish to replace, and examples of the content you would like to replace?

As a guess based on what you have called your variables, line 2 above should be searching for the placeholders within the original content, not the replaced content, as the placeholders won't exist in the replaced content. Also, remove the ^ and $, as these identify the start and end of a line. I shouldn't have included these - sorry.

I expect what you'll eventually have to do is find all of the placeholders, with numerical suffixes using preg_match_all. Then iterate over the array that is returned replacing each value in turn.

E.g. And this is untested:

preg_match_all('/(blah):(\d+)/', $content, $matches);

foreach($matches as $match) {
    $placeholder = "{$match[1]}:{$match[2]}";
    $content = str_replace($placeholder, $replacedContent, $content);
}

echo $content;

With more specific example content, this could be tweaked further.

R.

0

Hi,

You can find out more about regular expression syntax via Google, or the PHP.net website.

Can you provide a specific example of the input text which includes the placeholders you wish to replace, and examples of the content you would like to replace?

As a guess based on what you have called your variables, line 2 above should be searching for the placeholders within the original content, not the replaced content, as the placeholders won't exist in the replaced content. Also, remove the ^ and $, as these identify the start and end of a line. I shouldn't have included these - sorry.

I expect what you'll eventually have to do is find all of the placeholders, with numerical suffixes using preg_match_all. Then iterate over the array that is returned replacing each value in turn.

E.g. And this is untested:

preg_match_all('/(blah):(\d+)/', $content, $matches);

foreach($matches as $match) {
    $placeholder = "{$match[1]}:{$match[2]}";
    $content = str_replace($placeholder, $replacedContent, $content);
}

echo $content;

With more specific example content, this could be tweaked further.

R.

0

sry been busy coding and now i have a break

ok a more specific example.

in $content = '
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
{nav:6}
</body>
</html>
';

then grab {nav:6} put it into a $match (there will only be once instance of this placeholder)

then collect the number in $numlimit

then put the number in a loop with max being the $numlimit, while appending the looped content to a variable called $loop_content

then replace loop content with the matched content

$replaced_content = str_replace(%match, $loop_content , $content);

echo $replaced_content;

hope that i am making sense. my brain has had alot to think about lately

Edited by itisnot_me: n/a

0

Hi,

Thanks for the example. You say to collect the number in $numlimit, and then put the number in a loop. Are you saying you want to replace the placeholder with the replacement content however many times are dictated by $numlimit?

E.g. {nav:3} might be replaced with '<li></li>' three times - hence '<li></li><li></li><li></li>'.

I just want to make sure I completely understand.

R.

0

yes. the idea is to tell the script that this is where the nav will be and this is the limit of how many nav items you can only have here.

0

This looks a bit mashed to me. I can't see how this works. Surely you have all the data you need in variables? Simply use a loop to echo out the navitems.

Anyway why not use str_repeat() when you get the number.

0

the idea is simplicity. i wouldnt need to code out the loop for a web template. all i need to do is use a placeholder with a limit and the script does the grease work. the data gets populated as it goes. and always changes depending on what page you are viewing (there are other placeholders but i have taken care of those). but the nav data needs to have a limit or you can break the template look. ex: {nav:4} this the nav information is stored in the DB.

Edited by itisnot_me: n/a

0

Ok, but I still can't see why you can't put a cap on the number of items the nav can hold at the start. If nav items for a page = to total number allowed - stop any more additions.

I assume you have to place data within the <li> tags at some point. Why can't you do the whole lot in one go?

I'm sure I'm missing the point here - it's late. Is it that you don't want any php in that particular file - just use it as a template?

If so, perhaps you want to use a templating engine like RainTPL (not trying to plug it honest).

0

i dont know anything about RainTPL. but i am trying to do templating system. so the designer only tells the cms that there can only be only a max number of links here. im making it so that what ever look you want to have you can make. Without programming in anything if you dont want to or really need to. the idea is to make the cms do most if not all the work that you would need in a normal site. with the exceptions of the custom code (which will run along with the templating system).

there are things out there like what you suggest above but im more of learn and create it yourself. this is a personal project along with production product. trying to make myself a better and more efficient coder

Edited by itisnot_me: n/a

0

OK, so I suppose storing all this info in the DB or config file ain't happening for you.

The way seem to be doing it, you have to duplicate all the various bits including the placeholders.

Why not:

index.php

$navitems = 5;
$title = "homepage";
$description = "blah blah blah";
$keywords = "blah, blah, blah";
//or get the above from a DB
$maincontent = "content/index.html";
include('templates/main.php');

about.php

$navitems = 5;
$title = "about me";
$description = "blah blah blah";
$keywords = "blah, blah, blah";
//or get the above from a DB
$maincontent = "content/about.html";
include('templates/main.php');

templates/main.php

DTD
<html>
<head>
 <title><?php echo $title;?></title>
 <!-- same for other head vars -->
</head>
<body>
 <div id="header">
 ...whatever common content...or an include file...
 </div>
 <div>
   include("includes/nav_generator");
 </div>
 <div id="side">
 ...whatever common content...or an include file...
 </div>
 <div id="main">
 include($maincontent);
 </div>
</body>
</html>

I really don't know if I'm barking up the wrong tree here. This is a simple (trivial) template system. Like I said earlier, you may be better off with a real templating engine like RainTPL or Smarty. Have a look at my site for a look at RainTPL if you're interested: http://www.diafol.org/rain.php

0

sry i dont think that you do get what i am doing. Im not hard coding anything in. except how to get the data. but that is really the only thing. its dynamic. and only uses one page and some classes. it is actually pretty well thought out. im proud of it

I got my templating system pretty well down. just working on a few things that i dont know how to do, so i can learn how to.

thanks for your help though. But im going to wait for blocblue to chime in.

0

Hi,

Sorry for taking a while to post another reply. Busy couple of days. Thanks for the extra information. I've put together the following:

<html>
    <head></head>
	<body>
		<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam enim erat, iaculis sit amet posuere eget, bibendum in velit.</p>
		<ul>{nav:6}</ul>
		<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam enim erat, iaculis sit amet posuere eget, bibendum in velit.</p>
		<ul>{footer:6}</ul>
	</body>
</html>
<?php
$template = file_get_contents('template.tpl');

// Extract placeholders and limits
preg_match_all('{([a-z]+):(\d+)}', $template, $matches, PREG_SET_ORDER);

// Call content replacement method(s)
foreach($matches as $match) {
	$templateMethod = 'replace' . ucfirst($match[1]);
	$limit = isset($match[2]) ? $match[2] : 1;
	$template = $templateMethod($template, $limit);
}

var_dump($template);


function replaceNav($template, $limit = 5) {
    $replace = str_repeat('<li>Nav Item</li>', $limit);
	return preg_replace('/{nav:[\d+]}/', $replace, $template);
}

function replaceFooter($template, $limit = 5) {
	$replace = str_repeat('<li>Footer Item</li>', $limit);
	return preg_replace('/{footer:[\d+]}/', $replace, $template);
}

I'm not sure what you had in mind, but my thought process was that the placeholder name could be used to call particular methods, such as that seen above, passing through the template, and the number of times items should be replaced.

This could be modified and combined into a single templating class. You could also pass additional parameters if required.

Is this heading in the right direction?

R.

0

NP i know what it is like being pretty busy. i mean i am workin on 3 projects at the same time. and i am the only programmer. and these are full fledged projects. haha

yep that was what i was looking for. you actually helped me out in more ways that one. i took what you had and tweeked it to my script and got it working. then i organized my code better so that it didnt look like a jumbled mess. then i learned more about preg than i did. I also learned a little bit about regular expressions. There was an error with

preg_match_all('{([a-z]+):(\d+)}'

it needed to have two /

preg_match_all('/{([a-z]+):(\d+)}/'

other than that it would/was grabbing my inline styles that i had in the html page.

thanks alot. you have taught me more and helped me with my project.

Edited by itisnot_me: n/a

0

Ah, sorry about the regex delimiters. I was rushing to answer at work. Glad it helped and that you learnt something new.

Good luck with the projects.
R.

0

Its just one of my things that when presented with me code i like to make sure i realize of it ticks before moving forward. hope that makes sense.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.