is it possible to do some kind of str_replace and replace the thing with a php string.
ex:

{name}

to

<? myrow['name']; ?>

Recommended Answers

All 5 Replies

in personalising a mailing, or web page,
& because php can build the page
its easy enough

if !isset($name) {$name='guest';}
echo "hello $name, How are you?";//dquotes
echo 'hello '.$name.', How are you?';//squotes
/* or */
// logic to get the row from the table
echo 'hello '.$myrow['name'].', How are you?';

data within the string and in quotes can be replaced.

is it possible to do some kind of str_replace and replace the thing with a php string.
ex:

{name}

to

<? myrow['name']; ?>

Well it sounds like you're trying to write a template parser in which case I'd recommend using a prebuilt one like Smarty or just searching for PHP template parser. However, if you want to go at it alone you can use regular expressions.

$sometext = preg_replace('/\s*\{(\w+?)\}\s*/i', '<?php echo $myrow[\'\1\']; ?>', $sometext);

so my understanding is i would just have to use preg_replace and escape the character i have in the string

$sometext = preg_replace('\{name\}', '<?php echo $myrow[\'name\']; ?>', $sometext);

cause i do not understand this

/\s*\{(\w+?)\}\s*/i

so my understanding is i would just have to use preg_replace and escape the character i have in the string

$sometext = preg_replace('\{name\}', '<?php echo $myrow[\'name\']; ?>', $sometext);

cause i do not understand this

/\s*\{(\w+?)\}\s*/i

/\s*\{(\w+?)\}\s*/i says anything that is { then the letters a-z or underscore then } so {name} or {state}, etc. and replaces it with whatever was between the {} so {state} would become <?php echo $myrow['state']; ?> and {name} would be come <?php echo $myrow['name']; ?> The way you're doing it is pointless to use preg_replace because its not a regular expression, its just a string and you could use str_replace

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.