Jump to content

preg_split help


The Little Guy

Recommended Posts

$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

Link to comment
https://forums.phpfreaks.com/topic/37715-preg_split-help/
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/37715-preg_split-help/#findComment-180438
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/37715-preg_split-help/#findComment-180440
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/37715-preg_split-help/#findComment-180441
Share on other sites

<?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?

Link to comment
https://forums.phpfreaks.com/topic/37715-preg_split-help/#findComment-180472
Share on other sites

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);
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/37715-preg_split-help/#findComment-180927
Share on other sites

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);
	}
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/37715-preg_split-help/#findComment-180953
Share on other sites

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);
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/37715-preg_split-help/#findComment-180965
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.