i have a page and its showing all the names with "-" character between them
and i want to just echo the first word before the - character and the second word after the - character
arsenal - liverpool
Betis - Athletic Bilbao
Waasland Beveren - Oostende
the code i tired
$arr1 = explode(' ',trim($Event["Event"]["name"]));
echo $arr1[0]."\n";
but this is just showing the first word , if the first word includes tow names then i cant echo it , i want to echo the full name before and after the - character in tow seprate php
i want to get this outputs from the examples
arsenal
liverpool
Betis
Athletic Bilbao
Waasland Beveren
Oostende
Try this:
$arr1 = explode('-', trim($Event["Event"]["name"]));
$team = $arr1[0];
$town = $arr1[1];
echo "$team - $town";
You are using the explode function with the wrong argument value, it should be used with a hyphen (-) instead of a space.
Note this will leave you with leading and trailing spaces, to remove this you can use the trim function:
$team = trim($arr1[0]);
$town = trim($arr1[1]);
Or put a space before and after the hyphen in the explode function:
$arr1 = explode(' - ', trim($Event["Event"]["name"]));
This is only the case if your string will always be in the format:
<team> - <town>
The explode() function breaks a string into an array.
Note: The "separator" parameter cannot be an empty string.
Use "-" separator in explode function.
<?php
$arr1 = explode('-',trim($Event["Event"]["name"]));
echo trim($arr1[0])."\n";
echo trim($arr1[1])."\n";
?>
Related
I'm trying to convert the following numbers, so that all have the same value of 5460 if there is no comma or dot used at the third last position or 5460,00 if there is a comma or dot.
Here are my test numbers:
5460
5.460
5.460€
5460,00
5460,00€
5460.00
5460.00€
5.460,00
5.460,00€
5,460.00
5,460.00€
I used the following regex with preg_replace:
preg_replace('/[^0-9\,\-]+/','',$number);
The result was the following
5460 -> 5460
5.460 -> 5460
5.460€ -> 5460
5460,00 -> 5460,00
5460,00€ -> 5460,00
5460.00 -> 546000 // wrong
5460.00€ -> 546000 // wrong
5.460,00 -> 5460,00
5.460,00€ -> 5460,00
I don't know how to optimize the regex, so that also the wrong values will be correct replaced like this:
5460.00 -> 546000 // wrong because should be 5460,00
5460.00€ -> 546000 // wrong because should be 5460,00
Test case:
$numbers = array('5460', '5.460', '5.460€', '5460,00', '5460,00€', '5460.00', '5460.00€', '5.460,00', '5.460,00€');
foreach ($numbers as $number)
echo $number." -> ".preg_replace('/[^0-9\,\-]+/','',$number) . "\n";
So I don't know how to check if the last two digits have a dot before, and if yes to replace it with a comma. But only for the last two digits.
Thanks
This does the job, but there's probably a more simple solution:
$numbers = array('5460', '5.460', '5.460€', '5460,00', '5460,00€', '5460.00', '5460.00€', '5.460,00', '5.460,00€');
foreach ($numbers as $k => $number) {
$number = preg_replace('~[.,](?=\d{2}\b)|\p{Sc}~u', '#', $number);
$number = strtr(rtrim($number, '#'), ['#' => ',', '.' => '', ',' => '']);
echo $numbers[$k], ' -> ', $number, PHP_EOL;
}
The pattern matches the currency symbol and the dot or the comma followed by only two digits. They are replaced with a dash.
For example : 5.460,00€ => 5.460#00#
Then the dash is stripped on the right, and with strtr remaining dashes are translated to commas and commas or dots to empty string at the same time.
Considering how many different ways there are to write currency (even just with Euros), you may struggle using a simple regex to identify the 'true' value people are intending to enter.
You may benefit from something like PHP's money_format() to automatically correct for any possible confusion, by setting the locale with setlocale():
setlocale(LC_MONETARY, 'de_DE');
$numbers = array('5460', '5.460', '5.460€', '5460,00', '5460,00€', '5460.00', '5460.00€', '5.460,00', '5.460,00€');
foreach ($numbers as $number)
echo money_format('%i', $number) . "\n";
This will automatically take any values inputted and convert them to the German equivalent. Keep in mind that you'll also want to strip out the Euro signs!
Hope this helps! :)
How to convert this:
"Elephant, Africa, landscape"
into this:
"elephant, Africa, landscape"
I tried using strlower and lcfirst php functions, but that is not what I wish. I wish just fist character of just first word to be lowercase, not all sentence lowercase or all words to be with first character lowercase.
Is there something to get first character of first word only lowercase?
UPDATE:
I wish to show post title as keywords, and I use this:
$title = get_the_title( $post->post_parent ); // Get post title
$parts = explode( ' ', $title ); // Delimetar for words in post title " "
$str = '';
foreach ($parts as $word) {
$str.= lcfirst(post_title_as_keywords($word)); // Lowercase first character
}
$str = substr($str, 0,-2);
echo $str;
And this is what I get:
"elephant, africa, landscape"
How to prevent lowercase effect on all words?
PHP Version: > 5.3.0:
The lcfirst() function converts the first character of a string to lowercase.
echo lcfirst("Elephant, Africa, landscape");
//output elephant, Africa, landscape
have a look at w3c schools
For PHP < 5.3 use
$your_string = "Elephant, Africa, landscape";
$your_string[0] = strtolower($str[0]);
//output elephant, Africa, landscape
UPDATE:
Use the function not inside your foreach-loop will fix your issue.
You're using lcfirst() once per word (inside the for loop). Try applying lcfirst() to str after the loop, instead of inside it:
$str = lcfirst(substr($str, 0,-2));
This question already has answers here:
How to make first letter of a word capital?
(5 answers)
Closed 7 years ago.
I have a variable like
$fullname = "dwayne-johnson";
How can I make the "d" of the first letter of the word dwayne and the "j" of the word johnson? like I want to make uppercase the first letter of the every word that is separated by dash, for example I have following variable (refer below)
$fullname1 = "dwayne-johnson" //expected result is, Dwayne Johnson
$fullname2 = "maria-osana-makarte" //expected result is, Maria Osana Makarte
as you can see from above the variable fullname1 have 2 words separated by dash and so the first letter in both words are capitalized. The second variable $fullname2 has 3 words separated by dash and so the first letter of each words within that variable is been capitalized. So how to make it capitalize the first letter of each words that is separated by dash in variable? Any clues, ideas, suggestions, help and recommendations is greatly appreciated. Thank you.
PS: i have already a function that convert the dash to space so now all I have to do is to make the first letter of every words that is separated by dash within the variable and the after it has been capitalized I will then inject the dash-space function on it.
Try with -
$fullname1 = ucwords(str_replace('-', ' ', $fullname1));
You can use the ucword function
Here's an example:
<!DOCTYPE html>
<html>
<body>
<?php
echo ucwords("hello world");
?>
</body>
</html>
<?php $text = str_replace("-", " ", $fullname1);
echo ucwords($text);?>
You can use the following code
$fullname = "dwayne-johnson";
// replace dash by space
$nospaces = str_replace("-", " ", $fullname);
// use ucword to capitalize the first letter of each word,
// making sure that the input string is fully lowercase
$name = ucword(strtolower($nospaces));
<?php
$fullname = "dwayne-johnson";
$fullname_without_dash = str_replace("-"," ",$fullname); // gives -> "dwayne johnson"
$fullname_ucword = ucwords($fullname_without_dash); //gives -> "Dwayne Johnson"
echo $fullname_ucword;
?>
A user from other thread help me to figure out how to get the numbers from an array, but now I can't get the numbers afer "-" dash. Let me show you what I have and put you in situation.
I''ve got an array with the next content:
Array(
[0] => <tr><td>29/06/2015</td><td>19:35</td><td>12345 Column information</td><td>67899 Column information - 12</td><td>Information</td><td>More information</td></tr>
[1] => <tr><td>12/03/2015</td><td>10:12</td><td>98545 Column information</td><td>67659 Column information - 32</td><td>Information</td><td>More information</td></tr>
[2] => <tr><td>11/02/2015</td><td>12:40</td><td>59675 Column information</td><td>94859 Column information - 11</td><td>Information</td><td>More information</td></tr>
[3] => <tr><td>01/01/2015</td><td>20:12</td><td>69365 Column information</td><td>78464 Column information - 63</td><td>Information</td><td>More information</td></tr>
)
Finally I know how to get every number (except the number after dash "-"):
$re = "/.*?(\\d+)\\s.*?(\\d+)\\s.*/m";
$str = "<tr><td>29/06/2015</td><td>19:35</td><td>12345 Column information</td><td>67899 Column information - 12</td><td>Information</td><td>More information</td></tr>";
$subst = "$1, $2";
$result = preg_replace($re, $subst, $str);
Here's the $result; output:
foreach($result as $finalresult) echo $finalresult.'<br>';
12345,67899
98545,67659
59675,94859
69365,78464
What I expected from all this process and cannot figure out is to get the number after dash "-" too:
12345,67899-12
98545,67659-32
59675,94859-11
69365,78464-63
But this does not end here... when the number after dash "-" is lower than 50 I need to transform the $result output. See the example below.
If the number after "-" < 50 then it needs to be transformed, taking the first digit and putting it at units position. Then the tens position might be zero.
When is 50 or above, the number ramains as it is. Example:
12345,67899-12 ------> 12345,67899-01
98545,67659-32 ------> 12345,67899-03
59675,94859-11 ------> 12345,67899-01
52375,53259-49 ------> 12345,67899-04
69365,73464-63 ------> 12345,67899-63
89765,12332-51 ------> 12345,67899-51
38545,54213-70 ------> 12345,67899-70
And now is when my head explodes!
Beforehand thanks a lot for your help.
This may be what you are looking for. I modified your regular expression slightly. The (.*?<td>){3} will match anything up to the third <td>. The ?P<first> in the subpattern (?P<first>\d+) etc. is called a named subpattern, which makes their value easy to access from the $matches array.
$a = [
'<tr><td>29/06/2015</td><td>19:35</td><td>12345 Column information</td><td>67899 Column information - 12</td><td>Information</td><td>More information</td></tr>',
'<tr><td>12/03/2015</td><td>10:12</td><td>98545 Column information</td><td>67659 Column information - 32</td><td>Information</td><td>More information</td></tr>',
'<tr><td>11/02/2015</td><td>12:40</td><td>59675 Column information</td><td>94859 Column information - 11</td><td>Information</td><td>More information</td></tr>',
'<tr><td>01/01/2015</td><td>20:12</td><td>69365 Column information</td><td>78464 Column information - 63</td><td>Information</td><td>More information</td></tr>',
];
$result = [];
foreach ($a as $row) {
$p = '#(.*?<td>){3}(?P<first>\d+).*?</td><td>(?P<second>\d+).*?(?P<third>\d+)#';
if (preg_match($p, $row, $matches)) {
if ($matches['third'] < 50) {
$matches['third'] = '0'.$matches['third'][0];
}
$result[] =
$matches['first'] . ',' .
$matches['second'] . '-' .
$matches['third'];
}
}
print_r($result);
Output:
Array
(
[0] => 12345,67899-01
[1] => 98545,67659-03
[2] => 59675,94859-01
[3] => 69365,78464-63
)
This will do the trick for you:
$re = '/.*?(\d+)\s.*?(\d+)\s.*?-\s(\d+).*/';
$str = "<tr><td>29/06/2015</td><td>19:35</td><td>12345 Column information</td><td>67899 Column information - 12</td><td>Information</td><td>More information</td></tr>";
preg_match($re, $str, $matches);
if ($matches[3]<50) $matches[3] = floor($matches[3]/10);
$format = '%d,%d-%02d';
$result = sprintf($format, $matches[1], $matches[2], $matches[3]);
echo $result;
Note that I changed your $re to be single quoted instead of double quoted for readability, and I'm using preg_match instead of preg_replace so I can work with the matched patterns.
To explain the regex to you, there are a few things going on:
/ is the regex delimiter.
.*?: The . tells the regex to match any character. The * says to do it zero or more times, and the ? says to do it in a "lazy" fashion. The plain .* at the end of $re matches the whole rest of the string.
(\d+): The \d is a wildcard telling the regex to match any digit. The + says "one or more times", and the () says to capture this. The first () surrounded group is $matches[1].
\s: Is a wildcard for any space character.
-: Is the literal - character.
Well... I don't know if it will help, but I made this with RegExr and it fits properly:
(([0-9]+){5})|(- [0-9]{2})
I hope you might find it some use!
I had a strange coding situation where I needed to have the URI become the title of the page being viewed. I couldn't think of another way to do it but now I need to format that URI and can't figure out how to accomplish it. It is a WordPress site so the URI is pretty clean. What I want to do is capitalize the letter of the first word and then a space, dash or pipe delimiter to separate out the title.
So this obviously gives me the URI:
<title><?php echo ($_SERVER['REQUEST_URI']) ?></title>
which gives me something like /test-catalog/diagnosis/flu. What I want to display is Test Catalog - Diagnosis - Flu
Thank you.
I would imagine this would work:
echo ucwords(str_replace(Array("-","/"),Array(" "," - "),$_SERVER['REQUEST_URI']);
By using str_replace and ucwords
Example
echo ucwords(str_replace('/', ' - ', str_replace('-', ' ', $_SERVER['REQUEST_URI'])));
several things to do:
$url = str_replace("-"," ",$url); // convert the - to spaces (do this first)
$url = str_replace("/"," - ",$url); // convert the / to hyphens with spaces either side
$title = ucfirst($url); // capitalize the first letter
if you want to capitalize each letter, then do this:
$title = ucwords($url); // capitalize first letter of each word
you might have some whitepsace begining and end, so do this:
$title= trim($title)
// remove the first slash '/'
$uri = substr($_SERVER['REQUEST_URI'], 1);
// ucwords to uppercase any word
// str_replace to replace "-" with " " and "/" with " - "
echo ucwords(str_replace(array("-","/"),array(" "," - "),$uri));
codepad
as a résumé of the previous answers:
echo ucwords(str_replace(array("-","/"),array(" "," - "),substr($_SERVER['REQUEST_URI'], 1)));