SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace all the elements of a string.

    Hi

    I am working on some basic stuff that will help me with HTMLentities. Unfortunately, my php seems to be quite rusty.

    Here is an array. Each key of the array, if found in text to be checked, will be replaced by its value. The list is quite long, but you don't have to worry about it:
    PHP Code:
    $HTMLentitiesList = array ( 
       
    ' ' => ' ',     '' => '³',     '' => 'Æ',    '' => 'Ù',
       
    '' => '¡',    '' => '´',    '' => 'Ç',   '' => 'Ú',
       
    '' => '¢',     '' => 'µ',    '' => 'È',   '' => 'Û',
       
    '' => '£',    '' => '¶',     '' => 'É',   '' => 'Ü',
       
    '' => '¤',   '' => '·',   '' => 'Ê',    '' => 'Ý',
       
    '' => '¥',      '' => '¸',    '' => 'Ë',     '' => 'Þ',
       
    '' => '¦',   '' => '¹',     '' => 'Ì',   '' => 'ß',
       
    '' => '§',     '' => 'º',     '' => 'Í',   '' => 'à',
       
    '' => '¨',      '' => '»',    '' => 'Î',    '' => 'á',
       
    '' => '©',     '' => '¼',   '' => 'Ï',     '' => 'â',
       
    '' => 'ª',     '' => '½',   '' => 'Ð',      '' => 'ã',
       
    '' => '«',    '' => '¾',   '' => 'Ñ',   '' => 'ä',
       
    '' => '¬',      '' => '¿',   '' => 'Ò',   '' => 'å',
       
    '*' => '­',      '' => 'À',   '' => 'Ó',   '' => 'æ',
       
    '' => '®',      '' => 'Á',   '' => 'Ô',    '' => 'ç',
       
    '' => '¯',     '' => 'Â',    '' => 'Õ',   '' => 'è',
       
    '' => '°',      '' => 'Ã',   '' => 'Ö',     '' => 'é',
       
    '' => '±',   '' => 'Ä',     '' => '×',    '' => 'ê',
       
    '' => '²',     '' => 'Å',    '' => 'Ø',   '' => 'ë',
       
    '' => 'ì',   '' => 'ò',   '' => 'ø',   '' => 'þ',
       
    '' => 'í',   '' => 'ó',   '' => 'ù',   '' => 'ÿ',
       
    '' => 'î',    '' => 'ô',    '' => 'ú',
       
    '' => 'ï',     '' => 'õ',   '' => 'û',
       
    '' => 'ð',      '' => 'ö',     '' => 'ü',
       
    '' => 'ñ',   '' => '÷',   '' => 'ý'
       
    ); 
    Here is how I tried to do get it working:
    1. (text simply not appearing on the page)
    PHP Code:
    echo ( strstr($displayText['pageText'], $HTMLentitiesList) ); 
    2. (text appearing on the page but no changes are made to chars)
    PHP Code:
    foreach ($HTMLentitiesList as $k => $v) {

       
    $text str_replace($HTMLentitiesList[$k], $v$displayText['pageText']);
       echo(
    $text);
       }*/ 
    Could anyone correct me where I went wrong?

  2. #2
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach ($HTMLentitiesList as $k => $v) {
     
    $text str_replace($k$v$displayText['pageText']);
     echo(
    $text);

    Your mind is like a parachute. It works best when open.
    (HH The Dalai Lama)

  3. #3
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cool
    thanks.

    could the same result be achieved with strstr?

  4. #4
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It *could* but it would still need a text replacement function - probably str_replace - and is therefore probably going to be more trouble than it is worth. If the code above works, I'd suggest you would need a good reason to use some other method.

    Your mind is like a parachute. It works best when open.
    (HH The Dalai Lama)

  5. #5
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whops...
    it still does not work:
    PHP Code:
       function checkHTMLentities ($text$replacestuff) {
      
       
    //replace each sign
     
       
    foreach ($replacestuff as $k => $v) {

       
    $formatText str_replace($k$v$text);

       }
       
       return 
    $formatText;
       
    }

    //see the above post for the $HTMLentitiesList array
    echo ( checkHTMLentities($displayText['pageText'], $HTMLentitiesList) ); 

  6. #6
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops. Sorry. My Bad.

    Should have been...

    PHP Code:
    $text $displayText['pageText']
    foreach (
    $HTMLentitiesList as $k => $v) {
    $text str_replace($k$v$text);
    }
    echo(
    $text); 
    Otherwise, each time you run the replacement, the changed is the last loop are lost.

    Essentially what you are saying there is.

    New string = replace "&" with "&" from "Cats & dogs are their masters' pets." = "Cats & dogs are their masters' pets."

    New String = replace "'" with "'" from "Cats & dogs are their masters' pets." = "Cats & dogs are their masters' pets."

    (The ampersand replacement is lost because your are re-assigning from the original string. You need to do cumulative replaces on the results of the previous iteration)

    Hope this makes sense to you, because I've confused myself here...
    Your mind is like a parachute. It works best when open.
    (HH The Dalai Lama)

  7. #7
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do one this in one of the following 2 ways

    1) Split your $HTMLentitiesListarray into 2 separate arrays. For example, I have called my arrays $HTMLSearch and $HTMLReplace

    PHP Code:
    $HTMLSearch[0] = ' ';
    $HTMLReplace[0] = ' ';
    $HTMLSearch[1] = '';
    $HTMLReplace[1] = '¬';
    $HTMLSearch[2] = '';
    $HTMLReplace[2] = '£';
    $src 'string not<> and pound<> and space< > and not<>';
    $cvt str_replace($HTMLSearch$HTMLReplace$src);
    echo 
    '--' $src '--<br />';
    echo 
    '--' $cvt '--<br />'
    2) or, as you asked above, use strtr - Here I have used your existing $HTMLentitiesList array
    PHP Code:
    $cvt strtr($src$HTMLentitiesList);
    echo 
    '--' $src '--<br />';
    echo 
    '--' $cvt '--<br />'
    Hope this helps

  8. #8
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot to you both.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •