i am trying to convert letters into array.
All the letters are coming from mysql results randomly
for($column=0;$column<8;$column++){
echo '<div class="'.bluexyz.'">'.
$field1 = mysql_fetch_object($sql)->code
.'</div>';
}
each individual letter is a div named bluexyz.
now i want to convert these letters into array.
i have used explode inside the forloop which is not working.
$array = explode('\n',$field1);
it is placing all the letters in the array index of [0]. i want to place a one letter in the one index.
It'd be easier if you provide a clearer explanation and provide an example of what you're fetching and what exactly you're expecting the output to be.
From what I understand, you're trying to convert the $field1 string into array.
You could use str_split() function here.
Try this:
$array = str_split($field1);
echo "<pre>";
print_r($array);
echo "</pre>";
Hope this helps.
Related
I am working with php. I have some dynamic string. Now I want to add some number after some string. Like, I have a string this is me (1). Now I want to add -7 after 1. So that string should be print like this this is me (1-7).
I have done this properly by using substr_replace. like this
substr_replace('this is me (1)','-59',-1,-1)
Now if there is more than one number like this this is me(2,3,1). I want to add -7 after each number. like this one this is me(2-7,3-7,1-7).
Please help. TIA
I dont know if there is a good way to do this in one or two lines, but the solution I came up with looks something like this:
$subject = "this is me (2,3,1)";
if (preg_match('[(?<text>.*)\((?<numbers>[0-9,]+)\)]', $subject, $matches)) {
$numbers = explode(",", $matches['numbers']);
$numbers = array_map(function($item) {
return $item.'-7';
}, $numbers);
echo $matches['text'].'('.implode(",", $numbers).')';
}
What happens here is the following:
preg_match checks whether the text is in our desired format
We generate an array from our captured named group numbers with explode
We add our "Magic Value" (-7) to every array element
We're joining the text back together
I have a text box.
I am enter the value in text box like 12 13 14.
and i am want to convert this into 12,13,14 and then convert it into array and show each separate value.
If your form field asks for the values without a comma, then you will need to explode the POST data by space. What you're doing now is imploding it by comma (you can't implode a string to begin with), and then trying to pass that into a foreach loop. However, a foreach loop will only accept an array.
$ar = explode(' ',$da);
That simple change should fix it for you. You will want to get rid of the peculiar die() after your foreach (invalid syntax, and unclear what you're trying to do there!), and validate your data before the loop instead. By default, if you explode a string and no matching delimiters are found, the result will be an array with a single key, which you can pass into a loop without a problem.
Are you sure you want to expect the user enters data in that particular format? I mean, what if the user uses more than one space character, or separate the numbers actually with commas? or with semicolons? or enters letters instead of numbers? Anyway.. at least you could transform all the spaces to a single space character and then do the explode() as suggested:
$da = trim(preg_replace('/\s+/', ' ', $_POST['imp']));
$ar = explode(' ', $da);
before your foreach().
use explode instead of implode as
The explode() function breaks a string into an array.
The implode() function returns a string from the elements of an array.
and you cannot do foreach operation for a string.
$da=$_POST['imp'];
$ar = explode(' ',$da);
foreach($ar as $k)
{
$q="insert into pb_100_fp (draw_3_fp) values ('".mysqli_real_escape_string($conn, $k)."')";
$rs=mysqli_query($conn, $q);
echo $k.",";
}
then you will get this output
o/p : 12,13,14,
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];
when using this code to fetch data from http://www.ea.com/uk/football/profile/Calfreezy/360 the code just echo's back the word 'Array'
Here is the code I'm using currently:
<?php
$content = file_get_contents("http://www.ea.com/uk/football/profile/Calfreezy/360");
preg_match('#<div class="stat">Titles Won<span>([0-9\.]*)<span class="sprite13 goalImage cup"></span></span>#', $content, $titleswon);
echo 'Titles Won: '.$titleswon.'';
?>
And this is the HTML I am trying to pull from the url:
<div class="stat">
Titles Won <span>0<span class="sprite13 goalImage cup"></span></span>
</div>
This is just returning Titles won: Array
When if working it should return Titles won: 0
What am I doing wrong, thanks.
You are printing the entire matches array instead of selecting the index(es) that you want from it and printing them.
See the documentation
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
preg_match() produces an array of matches. If you print out an array in string context, you get Array as the text. e.g.
$arr = array('foo' => 'bar');
echo $arr; // prints "Array"
echo $arr['foo']; // prints "bar"
preg_match returns an array as can be seen from the documentation.
if you want to see all the contents of the array use
var_dump( $titleswon );
If you just need the matched, you have to address that specific part.
The best approach would be :
if (preg_match('#<div class="stat">Titles Won<span>([0-9\.]*)<span class="sprite13 goalImage cup"></span></span>#', $content, $titleswon)) {
echo 'Titles Won: '.$titleswon[1].'';
}
The third param tho preg_match is passed by reference and will contain an array with matches in every capture group. You are using two "groups". The whole match and ([0-9\.]*) which will be the second. So I expect you need this:
echo 'Titles Won: '.$titleswon[1].''; // note the array is indexed by 0
If you have a look to the preg_match documentation:
http://php.net/manual/en/function.preg-match.php
You can see that in the $match argument is actually an array, $match[0] containing the whole match, and the consecutive array elements containing the subqueries.
If you do var_dump($titleswon) or print_r($titleswon) you will see the whole information, then you can access to the desired info, in your case it would be $titleswon[1]
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>";
}
?>