SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    pre_replace vs str.replace

    Hi there,

    I would like to truncate string avoiding cutting off words

    in php I could use something like:
    Code:
    if (strlen($description) > $maxchars){
    	$smalldesc = substr($description, ' ', $maxchars);
    	$smalldesc = preg_replace('/ [^ ]*$/', '...', $smalldesc);
    }
    I do however need to make this work in js (and I am only starting to learn js)

    my script looks like this so far:

    Code:
    if (document.adminForm.description.value!=""){
    	if (document.adminForm.description.value.length>=($maxchars-3)){
    		document.adminForm.smalldesc.value=document.adminForm.description.value.substr(0,$maxchars-3) + "...";
    	} else {
    		document.adminForm.smalldesc.value=document.adminForm.description.value;
    	}
    } else {
    	document.adminForm.smalldesc.value="";
    }
    trying to adjust that to reflect the functionality in the php snippet doesn't work
    Code:
    if (document.adminForm.description.value!=""){
    	if (document.adminForm.description.value.length>=($maxchars-3)){
    		document.adminForm.smalldesc.value=document.adminForm.description.value.substr(0,$maxchars-3);
    		document.adminForm.smalldesc.value=str.replace(/ [^ ]*$/, document.adminForm.smalldesc.value)+ "...";
    	} else {
    		document.adminForm.smalldesc.value=document.adminForm.description.value;
    	}
    } else {
    	document.adminForm.smalldesc.value="";
    }
    could somebody please point out to me how to get this to work?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Something like this?

    Code javascript:
    var form = document.adminForm,
        description = form.elements.description,
        smallDesc = '';
    if (description.value > '') {
        if (description.value.length >= $maxchars - 3) {
            smallDesc = description.value.substring(0, $maxchars)
                .split(' ') // divide up into separate words
                .slice(0, -1) // remove the last (partial) word
                .join(' ') // reform in to a string
                + '...'; // add on ellipses
        }
    }
    form.elements.smalldesc.value = smallDesc;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    positively so! works like a charm! thanks bunches! - also for the hints

    would you mind an explanation of the difference between the substring and the substr methods ? the results appear to being identical.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    substring uses values that indicate the start and stop position.
    substr uses the start position and length instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks again for the clarification!


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
  •