String Problems

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
PnHoPob
Forum Newbie
Posts: 16
Joined: Thu Nov 10, 2005 7:16 pm

String Problems

Post by PnHoPob »

Hey all!

Although I'm not a noob programmer, I'm a noob PHPer :( </ego boost>

Hmm.. how can I explain this? Okay, so I'm trying to this this convertion program that would take a string like this:
HDKRFG:DJFGKJN &GFKDJN& : DSFLKJNDSF&DJNDF:GSG
and convert it with something else.

Now, lets say I'm trying to convert all "DJ"s into "HG"s. that would be:
HDKRFG:DJFGKJN &GFKDJN& : DSFLKJNDSF&DJNDF:GSG

However, I need to ignore everything between two &s and an & and a : . So, this is what would have to be ignored:
HDKRFG:DJFGKJN &GFKDJN& : DSFLKJNDSF&DJNDF:GSG

Everything in olive is ignored, thus only the "DJ" in green has to be converted.


How in the world am I gonna do that? I've been trying to work with str_replace, but have gotten nowhere :(

</ego degration>

Also: is there a function that finds and/or deletes the first few characters in a string?
Last edited by PnHoPob on Sat Nov 12, 2005 8:39 pm, edited 1 time in total.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

you're better off using preg_* functions for that...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Regexes would be your friend for that problem.

As for deleting the first few characters.... substr could work there.

Code: Select all

$newstring = substr($oldstring,3,100);
3 = the character you wish to start at (removing the first 2 characters) and reading till the 100th character.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
PnHoPob
Forum Newbie
Posts: 16
Joined: Thu Nov 10, 2005 7:16 pm

Post by PnHoPob »

Cool, preg_s and regexes are working well :)

Here's another problem:
I want every [0-9]*[A-Z] to become [0-9][A-Z]. So something like 7*B can become just 7B.

Here's what I've been trying:

Code: Select all

$search = array ('@[0-9]*[A-Z]@');
    $replace = array ('@[0-9][A-Z]@');
    $text = preg_replace($search, $replace, $string);
But of course my output is:

Code: Select all

@[0-9][A-Z]@

What could I do that actually works :P
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Maybe this (main thing is probably escaping the * as that means something special to the regex):

Code: Select all

$search = array ('@([0-9])\*([A-Z])@');
$replace = array ('\1\2');
$text = preg_replace($search, $replace, $string);
Mac
PnHoPob
Forum Newbie
Posts: 16
Joined: Thu Nov 10, 2005 7:16 pm

Post by PnHoPob »

So \1 and \2 output [0-9] and [A-Z]? Niice, exactly what I needed, then :)
Thanks so much! I've been asking around like 4 forums and IRC chats and no one could figure it out!
PnHoPob
Forum Newbie
Posts: 16
Joined: Thu Nov 10, 2005 7:16 pm

Post by PnHoPob »

Urgh, here's another problem:

How can I count have many times something was preg_replaced?
Like,

Code: Select all

<?php
	$text = "There are lots of ees"
	$match = array('@e@');
    	$replace = array('E');
    	echo preg_replace($match, $replace, $text);
?>
That should echo "ThErE arE lots of EEs".
How can I make it echo 5, since "e" was replaces 5 times by "E"?
Post Reply