Jump to content

Recommended Posts

I want to limit this tag to 25 characters when file runs, now it can be anywhere from 7-40 characters

$teams = explode(" v ",$hie->Name);

 

 

 

foreach ($xml->xpath("//Competition[@id='NBA' and @sprtyp='BK']/Match") as $hie) {
    $timestamp = strtotime($hie->Date);
    $day = date('d' , $timestamp);
    $month = date('M', $timestamp);
    $time = date('h:i A', $timestamp-$timeDiff);
    $teams = explode(" v ",$hie->Name);

Link to comment
https://forums.phpfreaks.com/topic/310203-code-for-limit-characters-to-25/
Share on other sites

My recommendation?  Don't do it. 

13 hours ago, jkane said:

$teams = explode(" v ",$hie->Name);

$teams is an array of Strings with, I assume, two elements (as in "A v B") so you don't want to restrict the number of elements there. 

 

If you restrict the length of the Name Xml element, then you run the risk of losing the second team entirely or even breaking your own code:

Arbitrarily cutting this at 25 characters, your code only just gets the ' v ' delimiter that the explode() function requires.  If you did the cutting at twenty characters, or if the former name were just two characters longer, then you would lose that delimiter and your code might well break, receiving only one element in the resulting $teams value.

$hie->Name = 'Arnold Schwarzenegger v Sylvester Stallone' ; 
                                      ^
                       1         2    |    3
              123456789012345678901234567890

 

I have to ask "why" you want to do this at all ...

If your application allows names to be longer than 25 characters, why are you truncating them? 

 

I suspect your Users would be less than impressed if they chose much longer names and your code just chopped them off at some arbitrary limit.

 

Just to clarify, do you want the name for team 1 to be 25 characters? And the name for team 2 to be 25 characters? So you would have 50 characters at most for both team names?

If so, you could try requinix's suggestion above. You already exploded the names. From there, you can use substr() to limit each name.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.