The Little Guy Posted February 9, 2007 Share Posted February 9, 2007 $example = preg_split('/(^\[url=.+?\].+?\[/url\])/m', $row['code'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($example as $examp) { // For page items... if (strpos($examp, '[page=') === 0) { echo preg_replace('/^\[url=(.+?)\](.+?)\[/url\]/', '<p><a href="\1">\2</a></p>', $examp); } // For code items... else { highlight_string($examp); } } Warning: preg_split() [function.preg-split]: Unknown modifier 'l' in /home/.marble/ryannaddy/snippets.tzfiles.com/snippet.php on line 56 line 56 is the first line Warning: Invalid argument supplied for foreach() in /home/.marble/ryannaddy/snippets.tzfiles.com/snippet.php on line 57 Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 9, 2007 Author Share Posted February 9, 2007 OK... Now I have this: <?php $example = preg_split('/(^\[url=.+?\])/m', $row['code'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($example as $examp) { // For page items... if (strpos($examp, '[url=') === 0) { echo preg_replace('/^\[url=(.+?)\](.+?)\[/url\]/', '<p><a class="redLink" href="$1">$2</a></p>', $examp); } // For code items... else { highlight_string($examp); } } ?> Warning: preg_replace() [function.preg-replace]: Unknown modifier 'r' in /home/.marble/ryannaddy/snippets.tzfiles.com/snippet.php on line 60 line 60 = echo preg_replace('/^\(.+?)\[/url\]/', '<p><a class="redLink" href="$1">$2</a></p>', $examp); Quote Link to comment Share on other sites More sharing options...
effigy Posted February 9, 2007 Share Posted February 9, 2007 Regular expressions are contained by delimiters, and modifiers follow the last delimiter, like so: /pattern/modifiers. You cannot use the delimiter within the pattern unless you escape it, because that throws the containment off. I suggest choosing delimiters that are not used in your pattern; the percent sign is usually a safe choice: %pattern%modifiers. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 9, 2007 Author Share Posted February 9, 2007 OK... That got rid of the error, but it doesn't do anything, I am just going off of the other piece of code you gave to me. now it is only outputting: [ url=http://snippets.tzfiles.com/examples/CAPTCHA/form.php] Example[/url] That was what was initially put in, but it should look like this: <p><a class="redLink" href="http://snippets.tzfiles.com/examples/CAPTCHA/form.php">Example</a></p> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 9, 2007 Author Share Posted February 9, 2007 Never Mind I figured it out it was my preg_split which now looks like this: $example = preg_split('/(^\[url=.+?\].+?\[\/url\])/m', $row['code'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); Quote Link to comment Share on other sites More sharing options...
effigy Posted February 9, 2007 Share Posted February 9, 2007 It's the data. The patterns were not set up to allow a space between "[" and "url". Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 9, 2007 Author Share Posted February 9, 2007 I know, I did that on the board cause the board thought it was a URL. Thanks I realized that I wasn't escaping the / in /url, that is why preg_split wasn't working, and same with preg_replace. Thanks for the help Quote Link to comment Share on other sites More sharing options...
effigy Posted February 9, 2007 Share Posted February 9, 2007 Ah; OK. When posting, you should be able to click "Additional Options..." and "Don't use smileys." to disable the bbcode. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 9, 2007 Author Share Posted February 9, 2007 <?php $code = preg_split('/(^\[page=.+?\])/m', $row['code'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($code as $line){ // For page items... if (strpos($line, '[page=') === 0) { echo preg_replace('/^\[page=(.+?)\]/', '<h3>\1</h3>', $line); } // For code items... else { highlight_string($line); } $codes = preg_split('/(^\[url=.+?\].+?\[\/url\])/m', $line, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($codes as $lines){ // For page items... if (strpos($lines, '[url=') === 0) { echo preg_replace('/^\[url=(.+?)\](.+?)\[\/url\]/', '<h4><a class="redLink" href="\1">\2</a></h4>', $lines); } // For code items... else { highlight_string($lines); } } } ?> Problem: http://snippets.tzfiles.com/snippet.php?id=6 under each heading: image.php form.php result.php it displays: [page=image.php] [page=form.php] [page=result.php] Those shouldn't be here, and at the very bottom I have this extra part, which shouldn't be there either: [url=http://snippets.tzfiles.com/examples/CAPTCHA/form.php]Example[/url] <?php session_start(); if($_SESSION['img_number'] != $_POST['num']){ echo'The number you entered doesn\'t match the image.<br> <a href="form.php">Try Again</a><br>'; }else{ echo'The numbers Match!<br> <a href="form.php">Try Again</a><br>'; } ?> How can I modify my first code to make that work properly? Quote Link to comment Share on other sites More sharing options...
effigy Posted February 9, 2007 Share Posted February 9, 2007 You're running two processes. First, is found and transformed, then isn't found, so the else passes it through highlight. You need to reorganize your if/elses into if/else if/else. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 9, 2007 Author Share Posted February 9, 2007 I am so close... It isn't formatting the xxx Any other hints for me? I want to try to figure this one out, so just give me some hints. <?php $code = preg_split('/(^\[page=.+?\])/m', $row['code'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($code as $line){ if (strpos($line, '[page=') === 0) { echo preg_replace('/^\[page=(.+?)\]/', '<h3>\1</h3>', $line); }else if(strpos($line, '[url=') === 0){ $codes = preg_split('/(^\[url=.+?\].+?\[\/url\])/m', $line, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($codes as $lines){ // For page items... if (strpos($lines, '[url=') === 0) { echo preg_replace('/^\[url=(.+?)\](.+?)\[\/url\]/', '<h4><a class="redLink" href="\1">\2</a></h4>', $lines); } } }else{ highlight_string($line); } } ?> Quote Link to comment Share on other sites More sharing options...
effigy Posted February 9, 2007 Share Posted February 9, 2007 Place this before your foreach, refresh, and view the source: print_r($code); Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 9, 2007 Author Share Posted February 9, 2007 There we go... It works perfect, at least on the page that I'm using it on. Was this what you had in mind? <?php $code = preg_split('/(^\[page=.+?\])/m', $row['code'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($code as $line){ $codes = preg_split('/(^\[url=.+?\].+?\[\/url\])/m', $line, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($codes as $lines){ if (strpos($lines, '[page=') === 0) { echo preg_replace('/^\[page=(.+?)\]/', '<h3>\1</h3>', $line); }else if(strpos($lines, '[url=') === 0){ if (strpos($lines, '[url=') === 0) { echo preg_replace('/^\[url=(.+?)\](.+?)\[\/url\]/', '<h4><a class="redLink" href="\1">\2</a></h4>', $lines); }else{ highlight_string($lines); } }else{ highlight_string($lines); } } } ?> Quote Link to comment Share on other sites More sharing options...
effigy Posted February 9, 2007 Share Posted February 9, 2007 I would compact it to: <?php $code = preg_split('/^(\[page=.+?\]|\[url=.+?\].+?\[\/url\])/m', $row['code'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($code as $line){ preg_match('/^\[([^=]+)/', $line, $matches); switch ($matches[1]) { case 'page': echo preg_replace('/^\[page=(.+?)\]/', '<h3>\1</h3>', $line); break; case 'url': echo preg_replace('/^\[url=(.+?)\](.+?)\[\/url\]/', '<h4><a class="redLink" href="\1">\2</a></h4>', $line); break; default: highlight_string($line); } } ?> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 9, 2007 Author Share Posted February 9, 2007 OK... That way looks much easier to add more BBCode, and maintain it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.