Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Cuting n' managing Strings

    Hi there,
    I have this Variable, $Texto, is a string to large actually is the text of an articla from a magazine column, and I dont want to show all the text in my Articles Index, so I want to cut it, lets say at the 100th character; does any one know how I can do it, some like de JS's split() function. And I also need to find the way to replace inside a string, something like JS's replace().
    Any help will be useful, thnx.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I wrote a function for someone long ago to essentially what you are asking. You can read about it in this thread

    article continued... ?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Teach a man to fish...



    When you have a question like that, ALWAYS check the language documentation first. It'll save ya lots of time waiting on bozos like us to respond!

    http://www.php.net/manual/en/


    PHP has several built-in functions for just this. They are:

    substr(string,start[,length ])

    str_replace(string,replacement,subject)


    There is also the entire "ereg" set of functions, used for complicated replaces.


    Examples of what you're doing:

    Code:
    PHP Code:
    $Texto "This is a whole bunch of text, blah blah, an article...."; $portion substr($Texto,0,15); // pull the first 16 characters 
    and
    Code:
    PHP Code:
    $haystack "I went to the store and bought a turtle."; $needle "turtle"; $replace_with "monkey"; $new_string str_replace($needle,$replace_with,$haystack); print $new_string// Prints: I went to the store and bought a monkey. 

    Hope that helps!

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I gess I just can't get any fish...

    Thnx for the replays guys, but now I have this problem:
    I Have de same old big string $TEXTO that has several <br /> after I nl2br it, but the thing now is that I just do not replace the repeated <br /> with what I want. I wan't to replace two br tags, a newline and an other two br tags for some character like *.
    I don't know why nl2br() does not remove the newlines from the string, and I don't know why this replace does not work at all.
    Code:
    $portionTEXTO= nl2br($TEXTO); 
    $BRs = "<br />";
    $VariosBRs = $BRs.$BRs."\n".$BRs.$BRs;
    $PuntosYAparte = ".".$BRs;
    $MarcaPropia = "|°*";
    $Nada=" ";
    $portionTEXTO = str_replace($VariosBRs, $MarcaPropia, $portionTEXTO);//this string_replace is the problem does not match the $VariosBRs
    $portionTEXTO = str_replace($BRs, $Nada, $portionTEXTO);
    $portionTEXTO = str_replace($MarcaPropia, $PuntosYAparte, $portionTEXTO);
    echo($portionTEXTO);
    I guess the problem is with some kind of Regular Expretion or some thing...
    Any hel will be usefull


  •  

    Posting Permissions

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