Forum Moderators: coopster

Message Too Old, No Replies

parse my own set of commands inside a string?

         

partha

3:35 am on Jul 17, 2005 (gmt 0)

10+ Year Member



I'm writing my own blog software, and I just want to be able to stick some commands into the body of a post and have PHP do some actions based on them.

so like if my post is:
=====

Today I took a walk and I didn't see anything interesting at all. Here's a picture of me taking a walk:
[][]pic¦21[][]

=====

then php would automatically replace the last line with an image #21 from my database

dreamcatcher

7:00 am on Jul 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



partha,

try the str_replace() [uk2.php.net] function.

$data = str_replace("[][]pic¦21[][]", "images/picture21.jpg", $data);

dc

dmorison

8:48 am on Jul 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Taking it a step further, you could use preg_replace() [uk.php.net] to search using a regular expression, and therefore be able to use the value of whatever was matched in the replacement.

For example;

$data = preg_replace( "[][]pic¦([0-9]*)[][]", "<img src='images/picture\\1.jpg'>", $data );

The \\1 in the replacement string means use whatever was matched in the first bracketed element of the search string.

Note: square brackets may have to be escaped (preceeded with \) in the match string, i'm not sure.