473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

regular expression problem (preg_replace)

hi all

using preg_replace

how can i replace the letter i in a string with nothing (delete it)
when it is the last letter or it is followed by an i?

i have products that are listed in a db with i or ii as in 320ii and i
want to strip out the i's at the end when displaying the product name

thanks

marc

Jan 3 '07 #1
7 2011
Rik
monomaniac21 wrote:
hi all

using preg_replace

how can i replace the letter i in a string with nothing (delete it)
when it is the last letter or it is followed by an i?

i have products that are listed in a db with i or ii as in 320ii and i
want to strip out the i's at the end when displaying the product name
With last latter, do you mean last letter in a word or the real last
character.
Word:
preg_replace('/i(\b|i)/i','$1',$string);
Dead last:
preg_replace('/i($|i)/si','$1',$string);

If you want to strip out ALL ending i's:
preg_replace('/i+(\b)/i','$1',$string);
--
Rik Wasmus
Jan 3 '07 #2
Rik
Rik wrote:
preg_replace('/i($|i)/si','$1',$string);
No need for the /s modifier offcourse...
--
Rik Wasmus
Jan 3 '07 #3

Rik wrote:
Rik wrote:
preg_replace('/i($|i)/si','$1',$string);

No need for the /s modifier offcourse...
--
Rik Wasmus
hi rik that works great for instances of a single i at the end of the
string (last character) but i also need it to strip out any i's that
occur next to other i's so for example:

230ii := 230, 230iiiiii := 230,
just as 230i := 230

thanks for your help

marc

Jan 3 '07 #4
Rik
monomaniac21 wrote:
Rik wrote:
>Rik wrote:
>>preg_replace('/i($|i)/si','$1',$string);

No need for the /s modifier offcourse...
--
Rik Wasmus

hi rik that works great for instances of a single i at the end of the
string (last character) but i also need it to strip out any i's that
occur next to other i's so for example:

230ii := 230, 230iiiiii := 230,
just as 230i := 230
The last one should work:
preg_replace('/i+(\b)/i','$1',$string);
--
Rik Wasmus
Jan 3 '07 #5
On Wed, 03 Jan 2007 07:37:02 -0800, monomaniac21 wrote:
hi all

using preg_replace

how can i replace the letter i in a string with nothing (delete it)
when it is the last letter or it is followed by an i?

i have products that are listed in a db with i or ii as in 320ii and i
want to strip out the i's at the end when displaying the product name

thanks

marc
if you have a huge database it might be better to use stored procedure :

(vars between "{}" have to be adapted to your table/field)

DELIMITER ?
CREATE PROCEDURE sp ()
BEGIN
DECLARE id INT;
DECLARE currentid INT DEFAULT 0;
DECLARE productname VARCHAR(128);
DECLARE newproductname VARCHAR(128);
DECLARE maxid INT DEFAULT 0;
SELECT max({product_id}) INTO maxid FROM {product} where {product_id} >
current AND {product_name} REGEXP 'ii$';

WHILE ( current < maxid+1 ) DO

SELECT {product_id},{product_name} INTO id,productname FROM {product}
where {product_id} current AND {product_name} REGEXP 'ii$' LIMIT 1;
newproductname = LEFT(productname,LENGTH(productname)-1);

UPDATE {product} set {product_name} = newproductname WHERE {product_id}
=id;

current=id;

END WHILE;
END?
DELIMITER ;
Jan 3 '07 #6
"monomaniac21" <mc******@googlemail.compíse v diskusním príspevku
news:11**********************@n51g2000cwc.googlegr oups.com...
hi all

using preg_replace

how can i replace the letter i in a string with nothing (delete it)
when it is the last letter or it is followed by an i?

i have products that are listed in a db with i or ii as in 320ii and i
want to strip out the i's at the end when displaying the product name
preg_replace('/^([^i]+)i*$/i','$1',$string);

320i will be 320
320ii will be 320
preg_replace('/^(.+)i*$/i','$1',$string);

320i will be 320
320ii will be 320i

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)


Jan 4 '07 #7
<?php
$regex = '
/
([^i]+) # product name before i. You could also negate \s to
validate, if necessary
i+(?:\b|$) # one or more is before a word boundary or end of line
/x';

$product = preg_replace($regex, '$1', $product);
?>

Just tested it, and it seems to work how the OP specified. This will
replace all i's following the product name:

$p = '320i 320ii 400 555iiii';

Becomes

320 320 400 555

Curtis

On Jan 3, 7:37 am, "monomaniac21" <mcyi2...@googlemail.comwrote:
hi all

using preg_replace

how can i replace the letter i in a string with nothing (delete it)
when it is the last letter or it is followed by an i?

i have products that are listed in a db with i or ii as in 320ii and i
want to strip out the i's at the end when displaying the product name

thanks

marc
Jan 4 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: LuKrOz | last post by:
Someone could tell me how can I get the same result substituting ereg with preg_match and ereg_replace with preg_replace. $result = ereg("<\>(.+)<\>",$this->buffer,$token); $this->buffer =...
2
by: Mo | last post by:
Have any idea about regular expression ? I posted this question to other newsgroup, but I'm still hungry for it. I tried to change any numeric characters to Zero like following; $aaa =...
9
by: jn | last post by:
I'm stripping out the attributes in <TD> tags...but I want to strip out everything BUT the COLSPAN attribute. The following strips out all attributes. What do I do if I want to keep a certain...
2
by: Clint Pachl | last post by:
Are backreferences inside look-aheads permitted? I am trying to remove a name=value pair from a url's query string using php's preg_replace. Here's what I am trying to accomplish: // query...
2
by: jojocrazy | last post by:
I'm definately not a regexp wiz.... need some suggestions here: grab PHP_SELF, strip the end filename off and the 2 directories preceeding it... This is what I've come up with so far, except...
5
by: Karin Jensen | last post by:
Hi I am writing in PHP and trying to work with regular expressions on records in a multilanguage database. I understand regexp basics, but have bitten off more than I can chew here and really...
3
by: Jake | last post by:
I am trying to write a regular expression that will strip out wikicode links. The links can be in the following two formats. ] or ] I want to display "xxx" in all cases. This is the closest...
3
by: vito | last post by:
I'm processing the following sequence with length more than 100k 1 cagatgctga taaaaaagtg tgttcctcat agcatttatt taattgaaat atttcaagaa 61 cttgaatgta ctaaaaattg agacaaacag tagcaaatca taaaaaaaaa...
2
by: windandwaves | last post by:
Hi Gurus, How can I write the thing below as a regular expression (preg_replace)? $array = array("-0", "-1", "-2", "-3", "-4"); $key = str_replace($array, "", $key); I am trying to learn...
0
by: peridian | last post by:
Hi, I wanted a web page where I could post code to, and have it appear in coloured formatting based on the context of the code. Most of the techniques I have seen for this involve complex use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.