You are here: Re: [PHP] Replacing 2 strings « PHP « IT news, forums, messages
Re: [PHP] Replacing 2 strings

Posted by Brian V Bonini on 11/02/91 11:17

On Sun, 2005-05-29 at 12:22, W Luke wrote:
> Hi,
>
> I have some text in a file which, when it's dumped to a var, needs to
> be replaced. In its raw form, it looks like this: <^JIM_JONES> and I
> need to remove the <^_ and > characters and have it read "Jim-Jones"
>
> It's nestled in amongst a load of other text - I'm fopen'ing a file
> and reading it line by line - the text-to-replace is just in a var
> named $text1
>
> Any ideas would be great
>
> Will

Someone much more clever that I can probably come up with something much
cleaner and efficient but.... This works...

<?php

function replace($string)
{
$chars = array("<", "^", ">");
$string = str_replace($chars, "", $string);
$string = str_replace("_", "-", $string);
$pieces = preg_split('/-/', $string);

foreach($pieces as $char) {
$first_letter[] = $char{0};
$remainder[] = strtolower(substr($char, 1));
}

$result = array_merge($first_letter, $remainder);
list($frstltr,$lstltr,$frstwrd,$lstwrd) = $result;
$string = $frstltr . $frstwrd . "-" . $lstltr . $lstwrd;
return $string;
}

$text1 = '<^JIM_JONES>';
echo replace($text1);

?>





--

s/:-[(/]/:-)/g


Brian GnuPG -> KeyID: 0x04A4F0DC | Key Server: pgp.mit.edu
======================================================================
gpg --keyserver pgp.mit.edu --recv-keys 04A4F0DC
Key Info: http://gfx-design.com/keys
Linux Registered User #339825 at http://counter.li.org

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация