PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Dec 16, 2017 5:27 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Thu Aug 22, 2013 4:39 am 
Offline
Forum Newbie

Joined: Thu Aug 22, 2013 4:12 am
Posts: 9
Hi,

I have the following string: ====== Text ======. As the number of the = characters may be from 2 to 6 on both sides of the text. I want to insert a dash like this: ====== -Text ======.
For this purpose I use the following code:
Syntax: [ Download ] [ Hide ]
$pattern = "/(={2,6})(.+?)(={2,6})/";
$replace = '$1 -$2$3';
$content = preg_replace($pattern, $replace, $content);
 

However, when I have some other string like this: ===============================, then a regex add dashes and in it.
I want to add dashes only when there is text between =(equal signs)

Regards!


Top
 Profile  
 
PostPosted: Thu Aug 22, 2013 5:54 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6404
Location: Montreal, Canada
. matches any character. Try restricting it to a range; [a-Z] or [a-zA-Z]

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Thu Aug 22, 2013 6:49 am 
Offline
Forum Newbie

Joined: Thu Aug 22, 2013 4:12 am
Posts: 9
Thanks for the answer!

I did this but does not work: $pattern = '/(={2,6})([a-zA-Z0-9_])(={2,6})/';
I tried and this: $pattern = '/(={2,6})(\w+)(={2,6})/';

But I have no success :(


Top
 Profile  
 
PostPosted: Thu Aug 22, 2013 8:03 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6404
Location: Montreal, Canada
Where are the spaces?

Code:
(={2,6}) ([a-zA-Z]+) (={2,6})

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Thu Aug 22, 2013 9:58 am 
Offline
Forum Newbie

Joined: Thu Aug 22, 2013 4:12 am
Posts: 9
Hi,
Š¢hanks for the help!
Here's the end result that works:
Syntax: [ Download ] [ Hide ]
/(={2,6}) ([a-zA-Z0-9_ ]+) (={2,6})/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group