473,657 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2031
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',$strin g);

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',$strin g);
No need for the /s modifier offcourse...
--
Rik Wasmus
Jan 3 '07 #3

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

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',$strin g);

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},{p roduct_name} INTO id,productname FROM {product}
where {product_id} current AND {product_name} REGEXP 'ii$' LIMIT 1;
newproductname = LEFT(productnam e,LENGTH(produc tname)-1);

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

current=id;

END WHILE;
END?
DELIMITER ;
Jan 3 '07 #6
"monomaniac 21" <mc******@googl email.compíse v diskusním príspevku
news:11******** **************@ n51g2000cwc.goo glegroups.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($r egex, '$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, "monomaniac 21" <mcyi2...@googl email.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
2695
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 = ereg_replace("<\>.+<\>","<>",$this->buffer) ; Thanks.
2
1816
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 = ereg_replace ("|0-9|", "0", $sentence); This seems working perfect. My question is how I can do this function only for outside of html tags. I don't want any numeric characters to be changed inside html tags.
9
3243
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 one? eregi_replace("<TD*>","<TD>", $string); I suck at regular expressions. I need some help.
2
2866
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 string tests $qs = 'var2=two&var1=one&var3=three'; // start $qs = 'var1=one&var2=two&var3=three'; // middle
2
1636
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 I'd like to replace the ..html for any extension and I've tried it but doesn't work... and I've also tried parsing backing a second directory but no luck, maybe you have an alternative solution:
5
1998
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 need help. The problem is to do with generating all strings that match a pattern defined in terms of brackets and slashes. Here, brackets mean that something is optional and slashes are used to show alternatives.
3
1264
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 that I have come up with $text = ereg_replace ("\]*\|)?(]*)\]\]", "\\2",$text);
3
1925
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 ttgaagtgaa 121 ttttacaact ggattcatgt gcctaatatt ttcattggga agtggattca tgtttaacat 181 ttccattggg <snippet> i wrote a program <?php
2
363
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 regular expressions, but I find them hard and this seems like a really simple place to start....
0
1947
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 of string manipulation where they seek through the string back and forth doing replacements to substitute in the needed HTML code. I am convinced that this can be done with a few regular expressions. Unfortunately my knowledge of regular...
0
8403
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8737
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.