Jump to content

Recommended Posts

Here I have a function:

function paragraphConvert($vehicleDescription)
{
	$string = $vehicleDescription;
	$toReplace[0] = '/[p]/';
	$toReplace[1] = '/[/p]/';
	$toReplace[2] = '/[u]/';
	$toReplace[3] = '/[/u]/';
	$replacement[0] = '<p>';
	$replacement[1] = '</p>';
	$replacement[2] = '<u>';
	$replacement[3] = '</u>';
	echo preg_replace($toReplace, $replacement, $string);
}

 

And here I have an error:

 

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'p' in C:\AppServ\www\tanksforsale\siteConfiguration.php on line 112

 

Basically If I have a block of text like this:

 

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque viverra. Vestibulum lorem. Cras laoreet suscipit lacus. Maecenas bibendum quam nec enim. Quisque luctus eleifend lorem. Sed massa ante, varius ac, elementum at, congue nec, lorem. Duis mi. Vivamus dolor justo, convallis at, dignissim quis, posuere interdum, mi. Sed euismod lacus vel tortor. In lacus pede, adipiscing in, auctor sed, molestie sit amet, turpis. Nunc hendrerit gravida nunc. Sed libero pede, convallis sit amet, tristique non, vulputate nec, lorem. Etiam ut arcu at pede consectetuer condimentum. Pellentesque ante odio, aliquam varius, fermentum vitae, sagittis ac, leo.

 

Vestibulum placerat scelerisque felis. Integer felis justo, interdum a, accumsan non, venenatis ut, felis. Mauris ut ante. Proin eu purus. Nunc ornare laoreet neque. Duis id erat. Curabitur est neque, tristique ac, molestie nec, blandit vel, pede. Nulla facilisi. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas porta eros a mi. Vestibulum cursus luctus eros. Fusce metus ipsum, pellentesque ac, sollicitudin at, auctor eget, neque. Sed quis odio. Maecenas at lacus sed libero ultricies aliquam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;

 

Vivamus sapien metus, rutrum in, placerat sed, laoreet ultrices, arcu. Phasellus sodales metus et enim. Morbi hendrerit, leo et accumsan placerat, nunc nibh sollicitudin metus, sed consectetuer purus sem at ligula. Quisque nec ligula at velit sodales blandit. Cras id purus. Mauris turpis tortor, interdum a, vestibulum eget, consequat vitae, magna. Nunc pellentesque purus vel felis. Cras dapibus. In ullamcorper molestie felis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed sed justo. Vestibulum ac eros sed massa imperdiet pellentesque. Suspendisse potenti.

 

I add it to a database and it turns out like this:

 

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque viverra. Vestibulum lorem. Cras laoreet suscipit lacus. Maecenas bibendum quam nec enim. Quisque luctus eleifend lorem. Sed massa ante, varius ac, elementum at, congue nec, lorem. Duis mi. Vivamus dolor justo, convallis at, dignissim quis, posuere interdum, mi. Sed euismod lacus vel tortor. In lacus pede, adipiscing in, auctor sed, molestie sit amet, turpis. Nunc hendrerit gravida nunc. Sed libero pede, convallis sit amet, tristique non, vulputate nec, lorem. Etiam ut arcu at pede consectetuer condimentum. Pellentesque ante odio, aliquam varius, fermentum vitae, sagittis ac, leo.

Vestibulum placerat scelerisque felis. Integer felis justo, interdum a, accumsan non, venenatis ut, felis. Mauris ut ante. Proin eu purus. Nunc ornare laoreet neque. Duis id erat. Curabitur est neque, tristique ac, molestie nec, blandit vel, pede. Nulla facilisi. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas porta eros a mi. Vestibulum cursus luctus eros. Fusce metus ipsum, pellentesque ac, sollicitudin at, auctor eget, neque. Sed quis odio. Maecenas at lacus sed libero ultricies aliquam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;

Vivamus sapien metus, rutrum in, placerat sed, laoreet ultrices, arcu. Phasellus sodales metus et enim. Morbi hendrerit, leo et accumsan placerat, nunc nibh sollicitudin metus, sed consectetuer purus sem at ligula. Quisque nec ligula at velit sodales blandit. Cras id purus. Mauris turpis tortor, interdum a, vestibulum eget, consequat vitae, magna. Nunc pellentesque purus vel felis. Cras dapibus. In ullamcorper molestie felis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed sed justo. Vestibulum ac eros sed massa imperdiet pellentesque. Suspendisse potenti.

 

Is there anyway I can combat this other than using my above (as yet not working) function?

Link to comment
https://forums.phpfreaks.com/topic/122096-solved-preg-replace-not-working/
Share on other sites

And here I have an error:

 

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'p' in C:\AppServ\www\tanksforsale\siteConfiguration.php on line 112

That's because you should escape the square brackets ( '[' and ']' ). Also it is pointless using preg_replace if you're not using regex within your patterns. This is how I'd do bbcodes:

function bbcode($string)
{
    $search[0] = '#\[p\](.*?)\[/p\]#ies';
    $search[1] = '#\[u\](.*?)\[/u\]#is';

    $replace[0] = "'<p>'.nl2br('$1').'</p>'";
    $replace[1] = '<u>$1</u>';

    return preg_replace($search, $replace, $string);
}

$text = '[p]Paragraph 1[/p]

[p]Paragraph 2, with [u]Underline![/u][/p]

[p]Paragraph 3,

keeps newlines![/p]';

echo bbcode($text);

I don't think he's trying to do bb codes I think he's trying to keep his text from losing his html tags so he thinks if he replaces them with non-html tags they will be marked and he can later convert them back...which is in essence what bb code does, but I think he's just wanting to preserve his format.

I don't think he's trying to do bb codes I think he's trying to keep his text from losing his html tags so he thinks if he replaces them with non-html tags they will be marked and he can later convert them back...which is in essence what bb code does, but I think he's just wanting to preserve his format.

Ohh, after re-reading the post I think you are right.

 

@IronWarrior ignore my post. The problem you're having is to do with the web browser ignoring newlines in your formatted text.

 

To prevent this you need to convert the newlines in your text to a line break tag (<br />). This can be achieved by using a built in function called nl2br. You should use this function when you retrieve data from your database only. Example

$sql = 'SELECT * FROM your_table';
$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result))
{
    echo nl2br($row['your_field_with_formatted_text']);
}

 

nl2br should not be used when adding data to the databases, as you may find your newlines duplicate each time you edit the text.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.