[SOLVED] Simple Problem!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Simple Problem!

Post by Joe »

OK I have posted something fairly similar to this before, however a couple of changes have been made. I am looking for a regular expression which could be used to take the number out of the braces. For example (£12.00) would become £12.00.

Any help appreciated here!


Joe 8)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$result = preg_replace('#\(([^\)]+?)\)#', '$1', $text);
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

feyd's answer is better.......

try something like this

Code: Select all

<?php
$var = preg_split ("/[()]+/", "(£12.00)"); 
echo $var[1];
?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Thanks feyd and dull much appreciated. Regular expressions is the one thing I need to have a look at. I noticed in the shop that there was actually huge books related on that one subject.
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

Why not simply using this one:

Code: Select all

<?php

$var = (€12.00);

$changeVar = array(")"=>"" , "("=>"");
$var = strtr($var,$changeVar);

?>
Antonie
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Well thanks for that. One more question though, is it possible for me to take the number out if there is text in the line. For example:

product1(£12.00)

Would become:

£12.00
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<?php

$result = preg_replace('#.*?\(([^\)]+?)\).*#s', '$1', $text);

?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Thanks feyd. Worked perfect. Problem solved. ;)
Post Reply