Im new here. I need help with php.
$FullDescriptionLine = Model 23MP48HQ-P|23"|Panel IPS|Resolution 1920x1080|Form factor 16:9|
how can i get 23" out of that string?
Thank you
Martin
Use PHP's explode function to split the string on pipes (|), then get the second index (1 in computer indexes) of that array.
$FullDescriptionLine = 'Model 23MP48HQ-P|23"|Panel IPS|Resolution 1920x1080|Form factor 16:9|';
echo explode('|', $FullDescriptionLine)[1];
Online PHP demo
Just explode() it with pipe | character and grab the first i.e 1st index as array starts from 0 index.
<?php
$FullDescriptionLine = 'Model 23MP48HQ-P|23"|Panel IPS|Resolution 1920x1080|Form factor 16:9|';
$array = explode('|',$FullDescriptionLine);
//just for debug and clearly understand it
print '<pre>';
print_r($array);
print '</pre>';
echo $array[1];
?>
DEMO: https://3v4l.org/8aGuO
For structured strings I recommend str_getcsv and then use the second parameter to define the delimiter.
$array = str_getcsv('Model 23MP48HQ-P|23"|Panel IPS|Resolution 1920x1080|Form factor 16:9|', '|');
echo $array[1];
https://3v4l.org/7XSDQ
You can use explode function. It converts a given string into array elements, separated by a delimiter. It takes two inputs, a delimiter string ('|') and the string to convert into array chunks ($FullDescriptionLine).
Now, in your case, 23" is in the second substring (array index 1 - remember that array indexing start from 0). Post exploding the string, you can get the value using index [1].
Try the following (Rextester DEMO):
$FullDescriptionLine = 'Model 23MP48HQ-P|23"|Panel IPS|Resolution 1920x1080|Form factor 16:9|';
// explode the string and access the value
$result = explode('|', $FullDescriptionLine)[1];
echo $result; // displays 23"
Using explode() is simple and good that multiple user posted it. But there is another way. Using regex in preg_match()
preg_match("/\|([^|]+)/", $FullDescriptionLine, $matches);
echo $matches[1];
Check result in demo
Related
this is my first post. sorry if i did something wrong...
anyways i have a file that gets updates by php and this is EXACTLY it:
31\n
127\n
131\n
124\n
144\n
142\n
133\n
133\n
9\n
0\n
22\n
18\n
i made this script in php:
$logContents = file_get_contents("logs/mainlog.txt");
$logitemArray = explode("\n", $logContents);
echo max($logitemArray);
but it echos 9. why? it said in the php documentation that max() should return the biggest value in the array
thanks in advance
explode() returns an array of strings, so they're being compared lexicographically. You need to convert them to numbers so that max() will compare them numerically.
$logitemArray = array_map('intval', explode("\n", $logContents));
echo max($logitemArray);
BTW, you can use the file() function to read a file directly into an array of lines, instead of using file_get_contents() followed by explode().
$logitemArray = array_map('intval', file("logs/mainlog.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
Like the comments have said, it's because 9 is the largest lexigraphical value. If it said 900 it would still be the same.
This is because when you split the string with explode you get an array of type string. The following code will convert the elements in the array to integers which should give expected behaviour.
$logitemArray = array_map('intval', explode("\n", $logContents));
How can I add string to another string after a specific character in PHP? Strings are coming from Database.
$stringDB= "FZE-17-01";
$string_add="RTL";
Final output= FZE-RTL-17-01
I tried functions but I don't want to use a position based function like substr_replace after 4 characters, etc. Any good alternative. $string_add after first -
One of many variants is to use array_splice
$arr = explode('-', $stringDB);
array_splice($arr, 1,0, $string_add);
echo implode('-', $arr);
Hope this could help you.
$stringDB= "FZE-17-01";
$string_add="RTL";
echo $newstr = substr_replace($stringDB, $string_add, 4, 0);
PHP substr_replace
I am new to php and trying out some different things.
I got a problem with printing a random value from a string from multiply values.
$list = "the weather is beautiful tonight".
$random = one random value from $list, for example "beautiful" or "is"
Is there any simple way to get this done?
Thanks!
well, as #Dagon suggested, you can use explode() to get an array of strings, then you can use rand($min, $max) to get an integer between 0 and the length of your array - 1. and then read the string value inside your array at the randomly generated number position.
// First, split the string on spaces to get individual words
$arg = explode(' ',"the weather is beautiful tonight");
// Shuffle the order of the words
shuffle($arg);
// Display the first word of the shuffled array
print $arg[0];
you can do numeric index in string like in array.
ex.
$text = "esenihc gnikcuf yloh";
echo $text[0];
echo $text[1];
echo $text[2];
...................
...................
...................
But if you put string in print_r() not same will happen like in array and you cant do count() with string.
I read the documentation and it says.
count()
return 1 if not an array in the parameter
print_r()
if string is in parameter it just prints that string.
this is not the exact word but something like this.
Why both these functions dont treat string same as an array?
So final question is string an array?
Unlike for example C, PHP has an inbuilt string datatype. The string datatype allows you array-like access to the single characters in the string but will always be a string. So if you pass it to a function that accepts the mixeddatatype this function will determine the datatype of the passed argument and treat it that way. That is way print_r() will print it in the way it was programmed to output strings and not like an array.
If you want a function that works does the same as count for arrays have a look at strlen.
If you want you can "turn" your string into an array through str_split.
A string is an array if you treat it as an array, eg: echo $text[0], but print_r Prints human-readable information about a variable, so it will output that variable.
It's called Type Juggling
$a = 'car'; // $a is a string
$a[0] = 'b'; // $a is still a string
echo $a; // bar
To count a string's length use strlen($string) then you can for a for()
no a string is no array
A string is series of characters, where a character is the same as a byte and An array in PHP is actually an ordered map. A map is a type that associates values to keys.
simply everything in the sense every variable in PHP is an array.
Maybe too late but:
<?php
$text = "esenihc gnikcuf yloh";
$arrText = explode(" ", $text);
foreach($arrText as $word) {
echo $word . "<br>";
}
?>
I've got a string of:
test1.doc,application/msword,/tmp/phpDcvNQ5,0,23552
I want the first part before the comma. How do I get the first part 'test1.doc' on it's own without the rest of the string?
The string came from an array I imploded:
$uploadFlag=implode( ',', $uploadFlag );
echo $uploadFlag;
If it's easier to extract just the first value off the array on it's own that would also do the job. I don't think the array has any keys.
Thanks in advance.
echo $uploadFlag[0];
Uh, try that in place of that whole chunk of code. Since you're imploding it, you could just grab the first piece instead. That ought to echo the proper value!
$parts = explode(',', $uploadFlag);
$firstPart = $parts[0];
Use this code:
$part = substr($uploadFlag , 0, strpos($uploadFlag , ','));
To extract it from the string, you can use preg_replace() for example.
$firstPart = preg_replace('/,.*$/', '', $uploadFlag);
In the above example, the regular expression replaces everything (.*) that follows the first comma (,) until the end of the string ($) with nothing ('').
Or, if you can use the $uploadFlag array before replacing it with the imploded string, then you can use reset() to go to the first element in the array and current() to extract its value.
reset($uploadFlag);
$firstPart = current($uploadFlag);
Implode is not the right function. It takes an array and combines into one string. You are trying to do the reverse operation, which is handled by explode:
$uploadFlag=explode( ',', $uploadFlag );
echo $uploadFlag;
echo array_shift(array_slice($uploadFlag, 0, 1)); will output the first element of your array beit an associative or numbered array.