jkane Posted March 4, 2020 Share Posted March 4, 2020 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); Quote Link to comment https://forums.phpfreaks.com/topic/310203-code-for-limit-characters-to-25/ Share on other sites More sharing options...
ginerjm Posted March 4, 2020 Share Posted March 4, 2020 A lone line of code and a block of meaningless and incomplete code that is doing something with an outrageous index name on an array? What IS this? And just what is a "tag" in your world here? Quote Link to comment https://forums.phpfreaks.com/topic/310203-code-for-limit-characters-to-25/#findComment-1575139 Share on other sites More sharing options...
requinix Posted March 5, 2020 Share Posted March 5, 2020 substr If $teams[0] or $teams[1] is less than 25 characters, substr() will return the whole value. Quote Link to comment https://forums.phpfreaks.com/topic/310203-code-for-limit-characters-to-25/#findComment-1575145 Share on other sites More sharing options...
Phi11W Posted March 5, 2020 Share Posted March 5, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310203-code-for-limit-characters-to-25/#findComment-1575161 Share on other sites More sharing options...
jkane Posted March 5, 2020 Author Share Posted March 5, 2020 Is it better I post a whole PHP file as an attachment? Yes I need team 1 and team 2 to 25 characters There’s a reason for this format better in the application Quote Link to comment https://forums.phpfreaks.com/topic/310203-code-for-limit-characters-to-25/#findComment-1575166 Share on other sites More sharing options...
cyberRobot Posted March 5, 2020 Share Posted March 5, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310203-code-for-limit-characters-to-25/#findComment-1575168 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.