Jump to content

[SOLVED] RegEx problem :( please help


marcusfaye87

Recommended Posts

I am trying to match a string that contains 41 or more characters in a row without a newline or space in it.

 

'/[^\n\s]{41,}/i'

 

that's what I have got for the regex, but it's not working :s it's still counting newlines and spaces.

 

any Idea's?

 

EDIT: I'm trying to do this because I don't want to allow things like:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

 

more than 40 characters without a space or linebreak messes up my layout.

Link to comment
Share on other sites

Yes, but I don't want it to always cap at that, just if a part of the string is longer than 40 characters to not allow it.

I'm also allowing youtube codes and such, since they are longer than 40 characters, they need to be allowed

 

<?php
	/** Handle tags */
	if ($post['type'] == 1)
	{
		$tempString = preg_replace ('/\[youtube\](.*)\[\/youtube\]/i', ' ', $post['body']);
		$tempString = preg_replace ('/\[img\](.*)\[\/img\]/i', ' ', $tempString);
	}
	else
	{
		$tempString = $post['body'];
	}

	$tempString = str_replace ('\r', '', $tempString);

	if (preg_match ('/[^\s]{41,}/i', $tempString))
	{
		$errors[] = 'Data overflow, your message cannot contain more than 40 characters without any spaces or linebreaks';
	}
?>

 

that's my code for it

Link to comment
Share on other sites

Well when I add a matches array and print it I get

 

Array ( [0] => \n012345678901234567890123456789\n0123456789 )

Data overflow, your message cannot contain more than 40 characters without any spaces or linebreaks

 

[The string I entered in the form is all nummeric to test, but as you can see it still catches the newlines]

 

code

<?php
	if (preg_match ('/[^\s]{41,}/i', $tempString, $matches))
	{
		print_r ($matches);
		$errors[] = 'Data overflow, your message cannot contain more than 40 characters without any spaces or linebreaks';
	}
?>

Link to comment
Share on other sites

<?php
	$tempString = str_replace ('\r', '', $tempString);
	$tempString = str_replace ('\n', '[newline]', $tempString);
	$tempString = str_replace ('\s', '[space]', $tempString);

	if (preg_match ('/[^(\[newline\]|\[space\])]{41,}/i', $tempString, $matches))
	{
		print_r ($matches);
		$errors[] = 'Data overflow, your message cannot contain more than 40 characters without any spaces or linebreaks';
	}
?>

this does work, which is weird

Link to comment
Share on other sites

<pre>
<?php
$tests = array(
	"0123456789\n0123456789\n0123456789\n0123456789\n0123456789\n0123456789",
	'abc def ghi jkl mnopqrstu',
	'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
	'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
	'ccccccccccccccccccccccccccccccccccccccccccc',
);
foreach ($tests as $test) {
	echo $test, '<br><b>';
	echo preg_match('/\S{41,}/', $test) ? 'Overflow!' : 'OK' ;
	echo '</b><hr>';
}
?>
</pre>

 

0123456789

0123456789

0123456789

0123456789

0123456789

0123456789

OK

abc def ghi jkl mnopqrstu

OK

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Overflow!

bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

OK

ccccccccccccccccccccccccccccccccccccccccccc

Overflow!

Link to comment
Share on other sites

I don't know why but your way doesn't work for me :s

What I mean is, if you enter EXACTLY 40 characters and then add a newline through the form, it won't match

 

<?php
	$tempString = str_replace ('\n', '[::&newline&::]', $tempString);

	if (preg_match ('/[^(\[\:\:\&newline\&\:\:\]|\s)]{41,}/i', $tempString))
	{
		$errors[] = 'Data overflow, your message cannot contain more than 40 characters without any spaces or linebreaks';
	}
?>

 

When I do it like that it works strangely enough, but if I just use \n it still counts the newline character to the string, making it 42

Link to comment
Share on other sites

What does this give you?

 

<pre>
<?php
$tests = array(
	str_repeat('a', 40) . "\n" . str_repeat('a', 40),
	str_repeat('a', 41) . "\n" . str_repeat('a', 41),
	str_repeat('a', 42) . "\n" . str_repeat('a', 42),
);
foreach ($tests as $test) {
	echo $test, '<br><b>';
	echo preg_match('/\S{41,}/', $test) ? 'Overflow!' : 'OK' ;
	echo '</b><hr>';
}
?>
</pre>

 

I get:

 

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

OK

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Overflow!

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Overflow!

 

You pattern will not work. Character classes ([...]) are only a list of characters from which one is picked, unless a quantifier is applied. It will not match strings of characters contained within.

Link to comment
Share on other sites

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.