Jump to content

Help with regex


eevan79

Recommended Posts

1). How to replace text with 3 tags:

1.

[ img ]

2.

images/smilies/smiley.gif

3.

[ /img] 

 

with "smiley.gif"  ?

 

example:

[img=http://...images/smilies/smiley.gif]  ->  smiley.gif

 

2). How to check if message contains only spaces? In script I allowed minmum 5 characters to post message. But when I press <space> 5 times I can post message. How to strip/wrap blank spaces?

Link to comment
Share on other sites

1). How to replace text with 3 tags:

1.

[ img ]

2.

images/smilies/smiley.gif

3.

[ /img] 

 

with "smiley.gif"  ?

 

example:

[img=http://...images/smilies/smiley.gif]  ->  smiley.gif

You'd use regex. Here is an example

$str = '[img=http://...images/smilies/smiley.gif]';

$str = preg_replace('/\[img\](.*)\[\/img\]/i', '<img src="$1" />', $str);

echo $str;

 

How to strip/wrap blank spaces?

Use trim

Link to comment
Share on other sites

Thanks.

In fact, that's what I did, but I made a mistake.

 

I just needed to put this code before I parse bbcode (html2bbcode).

What about second question - how to remove spaces if there is more than 3 in row (example "  ")?

 

EDIT:

for first question:

Do I have to change all smilies, or can I get just filename and remove link and BBC tags? Example from img and I want to get smiley.gif ...

Link to comment
Share on other sites

Thanks for answer, but that doesnt solve problem.

 

When I write 20 spaces and one character script counts 21 characters. If I write only spaces script doesnt allow to post less than 3 characters.

 

So I can post if I type "                            1 ", but not "                        ".

Can I fix this with regex?

Link to comment
Share on other sites

Reduce multiple spaces with regex

$str = 'h              e                                             l                                   l                                o                      !';
$new_str = preg_replace('/\s+/', ' ', $str);

echo "<pre>$str\n". strlen($str) . "\n\n$new_str\n" . strlen($new_str) . "</pre>";

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.