开发者

Separating certain keys from an Array into two clearly distinct groups. How?

开发者 https://www.devze.com 2023-02-19 20:30 出处:网络
I would like to add stuff out of an array and translate them. However, some items in the array I do not want to translate and so encapsulate them into special tags. In this example i\'ve uset < >

I would like to add stuff out of an array and translate them. However, some items in the array I do not want to translate and so encapsulate them into special tags. In this example i've uset < > but if you have suggestions/better iead's I'm open to them. I also thought of { } or [ ] and * * whichever you think is easiest.

<?php
# define the contents of my all time clients
$clients = array('<UNESCO>开发者_如何学Go;', 'Vrije Universiteit medical center');

# randomization engine
$keys = array_rand($clients, 3);    // get 3 random keys from your array
foreach ($keys as $key) {           // cycle through the keys to get the values

   # if this item is inbetween < > tags..
   if #??#######??########??#########??########??##

      # then put this item directly into randomList without translation
      $randomList .= "• " . $clients[$key]) . "<br/>";  // WORKS

   # all other items without <tags> are first translated via __() then added
   else $randomList .= "• " . __($clients[$key]) . "<br/>";  // WORKS
}?>

Question: What should be on line 10 to make the IF statement complete?

In other words, what is the logic in programming code that can match an item in the array being one of those special words that should not be translated and treaded differently?


Few possible solutions:

if (preg_match('~^<.*>$~', $clients[$key]))

if (strlen($clients[$key]) > 0 && $clients[$key][0] == '<' && substr($clients[$key], -1) == '>')


You probably want to ditch the brackets before outputting the name. You could use this then:

// note that you need to test against $clients[ $key ], not $key
if( preg_match( '~^<(.*)>$~', $clients[ $key ], $matches ) )
{
    $randomList .= "• " . $matches[ 1 ] . "<br/>";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消