CodeIgniter Forums

Full Version: problem with [coder] tags in article
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Hi all,

Thing I want to do is to use
on text between [coder][/coder] tags.

got kind of a problem with preg_match (that was first thing I found to use...)

I didn't find any CI function that would do what I want (I was looking in text helper).

here is the code...

$str = 'bla bla bla [coder]#!/bin/bash
cat banner.txt
select opt in $OPTIONS; do
if [ "$opt" = "QUIT" ]; then
echo Bye...
elif [ "$opt" = "SERVER1" ]; then
ssh [email protected]_IP
elif [ "$opt" = "SERVER2" ]; then
ssh [email protected]_IP
elif [ "$opt" = "SERVER3" ]; then
ssh [email protected]_IP
echo "error, no such option"
done[/coder] bla bla bla';

$ref = preg_match("/[coder](.*)[\/coder]/", $str, $matches);

$string = highlight_code($matches[1]);

echo $string;

but output on page is only oder]#!/bin

Did I overlook some CI function or smthn, and if there isn't any what is wrong with my code...

tnx in advance

El Forum

El Forum

[eluser]GSV Sleeper Service[/eluser]
you need to escape the square brackets too, also "." will not match newlines.

El Forum

[quote author="Vlad0" date="1228165096"][/quote]

@Vlad (cepes Smile )

do I really need to integrate complete helper in CI so I could just use one tag

escaping square brackets gives me error when returning data from array ...

will try to get the solution from that BB helper...

El Forum

[quote author="double_d" date="1228165872"]escaping square brackets gives me error when returning data from array ...[/quote]

because no matches are found.
you should check if the expression matched anything before working with the "result":

if ( preg_match(/*....*/) ) {
    // your code

your expression won't match anything containing newlines because
the appropriate pattern modifier is missing Smile

El Forum

$ref = preg_match("/\[coder](.*)\[\/coder]/sim", $str, $matches);

works as a charm Smile tnx allot