7 Replies - 477 Views - Last Post: 04 July 2011 - 06:38 AM Rate Topic: -----

#1 oneal.michaels  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 25-June 10

Function not outputting as expected

Posted 03 July 2011 - 07:15 PM

function bb_parse($string) {
    while (preg_match_all('`\[(.+?)=?(.*?)\](.+?)\[/\1\]`', $string, $matches)) foreach ($matches[0] as $key => $match) {
        list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]);
        switch ($tag) {
            case 'quote': $replacement = "<blockquote>$innertext</blockquote>" . $param? "<cite>$param</cite>" : ''; break;
        }
        $string = str_replace($match, $replacement, $string);;
    }
    return $string;
}



So what I expected is that when give the function something like

"[quote=name]quote[/quote]"

//it would output

"<blockquote>$innertext</blockquote> <cite>name</cite>"

//but instead it only returns

"<cite>name</cite>"

//when a parameter is present



does anybody see where the error is at, ive played around with the code and cant figure out whats wrong with it

Is This A Good Question/Topic? 0
  • +

Replies To: Function not outputting as expected

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • Joined: 08-August 08

Re: Function not outputting as expected

Posted 03 July 2011 - 07:51 PM

What are you passing to the function?
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Function not outputting as expected

Posted 03 July 2011 - 07:52 PM

Hey.

This would be the problem:
$replacement = "<blockquote>$innertext</blockquote>" . $param ? "<cite>$param</cite>" : ''; 



The . operator has higher precedence than the ternary operator, ?, so the line is being read as if you had done this:
$replacement = ("<blockquote>$innertext</blockquote>" . $param) ? "<cite>$param</cite>" : ''; 


Which will always return the <cite>.

You need to enclose your ternary statement in parenthesis in order for PHP to know in which order you want it executed.
$replacement = "<blockquote>$innertext</blockquote>" . ($param ? "<cite>$param</cite>" : ''); 


Was This Post Helpful? 1
  • +
  • -

#4 oneal.michaels  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 25-June 10

Re: Function not outputting as expected

Posted 03 July 2011 - 08:15 PM

thank youu! i was afraid i was going to have to waste another whole line of code to get it done :) yourr great!
Was This Post Helpful? 0
  • +
  • -

#5 oneal.michaels  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 25-June 10

Re: Function not outputting as expected

Posted 03 July 2011 - 09:13 PM

so i have another problem, when i try to match 3 new line feeds in a row or carriage returns like this:


$string = str_replace("\n\n\n", "<hr />", $string);

//or

$string = str_replace("\r\r\r", "<hr />", $string);




nothing happens, either way i try it
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Function not outputting as expected

Posted 03 July 2011 - 09:22 PM

What's the original value of $string?
If there were three \n or \r chars in there, that code would replace it. So that must not be what is there.

If these are actual new-lines, as created by a text editor, you are leaving out one possibility. Windows uses \r\n for new-lines, so if a Windows text-editor were to enter three new-lines, it would be: \r\n\r\n\r\n, which neither of your replacements would catch.

You would have to add a third replacement, or just do it all at once in a regular expression:
$string = preg_replace('/\r{3}|\n{3}|(\r\n){3}/', '<hr />', $string);


Was This Post Helpful? 2
  • +
  • -

#7 oneal.michaels  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 25-June 10

Re: Function not outputting as expected

Posted 03 July 2011 - 09:36 PM

thank you again!!! $string was input from a text field, and i had to use
$string = str_replace("\r\n\r\n\r\n", "<hr />", $string);
to find the returns
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • Joined: 08-August 08

Re: Function not outputting as expected

Posted 04 July 2011 - 06:38 AM

A decent text editor will let you choose between Unix and Windows formats so you don't have to worry about changing that code if/when you move it to a Unix server from your test PC. I believe Notepad++ will do this. On the Mac, TextWrangler does.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1