What does this code do? - php

<a href="http://www.google.com/search?q='.urlencode(current(explode('(', $ask_key))).'" target="_blank">
I can't understand what urlencode(current(explode('(', $ask_key ))) does.
Can anybody explain me what that code does?

explode the string $ask_key into an array using ( as the delimiter (so if the value of $ask_key is a(b(c, then array('a', 'b', 'c') will be returned.
and grab the first, i.e., current (as a new array will be pointing to its first element), element of that array
then urlencode it (making it safe for use in a query string).

$array is an string, that must contain several values, separated by (.
explode() will split this string into an array, using ( as a separator.
current() will get the current element of the array -- the first one.
and, finally, urlencode() will encode special characters, so they can be used in an URL.
So, basically :
Take the first element of a string such as these(are(elements
apply the urlencode function to it, so it can be used in an URL.
As an example, here is the same kind of code, split into several distinct operations, using a variable to store the result of each function -- so we can dump those results :
$string = "th#is?i&s(a couple(of elements";
var_dump($string);
$array = explode('(', $string);
var_dump($array);
$first_item = current($array);
var_dump($first_item);
$encoded = urlencode($first_item);
var_dump($encoded);
The four var_dump() will give this output :
string 'th#is?i&s(a couple(of elements' (length=30)
array
0 => string 'th#is?i&s' (length=9)
1 => string 'a couple' (length=8)
2 => string 'of elements' (length=11)
string 'th#is?i&s' (length=9)
string 'th%40is%3Fi%26s' (length=15)
Which shows in details what each portion of your expression does.

$ask_key = 'as das df(sdfkj as(asf a152451(sdfa df1 9'; //you key
echo $ask_key."<br/>";
$array = explode('(', $ask_key); //explode will split the array on '('
echo "<pre>";
print_r($array);
echo "</pre>";
$curr = current($array); //current will return the curr element of array
echo $curr."<br/>";
$enc = urlencode($curr); //url will encode url i.e. valid url
echo $enc;
Result::
as das df(sdfkj as(asf a152451(sdfa df1 9
Array
(
[0] => as das df
[1] => sdfkj as
[2] => asf a152451
[3] => sdfa df1 9
)
as das df
as+das++df

Related

How to split string with special character (�) using PHP

Currently I am trying to split the � the special character which represents %A0 at the URL. However when I use another URL, it doesn't recognize %A0 therefore I need to use %20 which is the standard space.
My question is. Is there a way to explode() special character �? Whenever I try to explode, it always return a single index array with length 1 array.
//Tried str_replace() to replace %A0 to empty string. Didn't work
$a = str_replace("%A0"," ", $_GET['view']);
// Tried to explode() but still returning single element
$b = explode("�", $a);
// Returning Array[0] => "Hello World" insteand of
// Array[2] => [0] => "Hello", [1] => "World"
echo $b[0];
Take a look at mb_split:
array mb_split ( string $pattern , string $string [, int $limit = -1 ] )
Split a multibyte string using regular expression pattern and returns
the result as an array.
Like this:
$string = "a�b�k�e";
$chunks = mb_split("�", $string);
print_r($chunks);
Outputs:
Array
(
[0] => a
[1] => b
[2] => k
[3] => e
)

explode string one by one character including space [duplicate]

This question already has answers here:
php explode all characters [duplicate]
(4 answers)
Closed 8 years ago.
how i can explode string into an array. Acctually i want to translate english language into the braille. First thing i need to do is to get the character one by one from a string,then convert them by mathing the char from value in database and display the braille code using the pic. As example when user enter "abc ef", this will create value separately between each other.
Array
(
[0] => a
[1] => b
[2] => c
[3] =>
[4] => e
[5] => f
)
i tried to do but not get the desired output. This code separated entire string.
$papar = preg_split('/[\s]+/', $data);
print_r($papar);
I'm sorry for simple question,and if you all have an idea how i should do to translate it, feel free to help. :)
If you're using PHP5, str_split will do precisely what you're seeking to accomplish. It splits each character in a string – including spaces – into an individual element, then returns an array of those elements:
$array = str_split("abc ef");
print_r($array);
Outputs the following:
Array
(
[0] => a
[1] => b
[2] => c
[3] =>
[4] => e
[5] => f
)
UPDATE:
The PHP4 solution is to use preg_split. Pass a double forward slash as the matching pattern to delimit the split character-by-character. Then, set the PREG_SPLIT_NO_EMPTY flag to ensure that no empty pieces will be returned at the start and end of the string:
$array = preg_split('//', "abc ef", -1, PREG_SPLIT_NO_EMPTY); // Returns same array as above
PHP has a very simple method of getting the character at a specific index.
To do this, utilize the substring method and pass in the index you are looking for.
How I would approach your problem is
Ensure the string you are converting has at least a size of one
Determine how long the string is
int strlen ( string $string )
Loop over each character and do some operation depending on that character
With a string "abcdef"
$rest = substr("abcdef", -1); // returns "f"
See the substr page for a full example and more information
http://us2.php.net/substr
You can iterate the string itself.
$data = 'something string';
for($ctr = 0; $ctr < strlen($data); $ctr++)
{
if($data{$ctr}):
$kk = mysql_query("select braille_teks from braille where id_huruf = '$data{$ctr}'");
$jj = mysql_fetch_array($kk);
$gambar = $jj['braille_teks'];
?>
<img src="images/<?php echo $gambar;?>">
<?php
else:
echo "&nbsp";
}

the fastest way to replace (and store in array) links in the text with their order numbers

There is a $str string that may contain html text including <a >link</a> tags.
I want to store links in array and set the proper changes in the $str.
For example, with this string:
$str="some text <a href='/review/'>review</a> here <a class='abc' href='/about/'>link2</a> hahaha";
we get:
linkArray[0]="<a href='/review/'>review</a>";
positionArray[0] = 10;//position of the first link in the string
linkArray[1]="<a class='abc' href='/about/'>link2</a>";
positionArray[1]=45;//position of the second link in the string
$changedStr="some text [[0]] here [[1]] hahaha";
Is there any faster way (the performance) to do that, than running through the whole string using for?
this can be done by preg_match_all with PREG_OFFSET_CAPTURE FLAG.
e.g.
$str="some text <a href='/review/'>review</a> here <a class='abc' href='/about/'>link2</a> hahaha";
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",$str,$out,PREG_OFFSET_CAPTURE);
var_dump($out);
Here the output array is $out. PREG_OFFSET_CAPTURE captures the offset in the string where the pattern starts.
The above code will output:
array (size=2)0 =>
array (size=2)
0 =>
array (size=2)
0 => string '<a href='/review/'>review</a>' (length=29)
1 => int 10
1 =>
array (size=2)
0 => string '<a class='abc' href='/about/'>link2</a>' (length=39)
1 => int 45
1 =>
array (size=2)
0 =>
array (size=2)
0 => string 'review' (length=6)
1 => int 29
1 =>
array (size=2)
0 => string 'link2' (length=5)
1 => int 75
for more information you can click on the link http://php.net/manual/en/function.preg-match-all.php
for $changedStr:
let $out be the output string from preg_match_all
$count= 0;
foreach($out[0] as $result) {
$temp=preg_quote($result[0],'/');
$temp ="/".$temp."/";
$str =preg_replace($temp, "[[".$count."]]", $str,1);
$count++;
}
var_dump($str);
This gives the output :
string 'some text [[0]] here [[1]] hahaha' (length=33)
I would use a regular expression to do such, check this:
http://weblogtoolscollection.com/regex/regex.php
try them here:
http://www.solmetra.com/scripts/regex/index.php
And use this:
http://php.net/manual/en/function.preg-match-all.php
Find your best regular expression to solve every case you may find: preg_match_all, if you set the pattern correctly, will return you an array containing every link you desire.
Edit:
In your case, assuming you want to keep the "<a>", this may work:
$array = array();
preg_match_all('/<a.*.a>/', '{{your data}}', $arr, PREG_PATTERN_ORDER);
Input example:
test
Lkdlasdk
llkdla
xx
Output with the above regexp:
Array
(
[0] => Array
(
[0] => test
[1] => Lkdlasdk
[2] => xx
)
)
Hope this helps

Spliting string based on length from end in PHP

I want to split a string from behind. Like:
$mystring = "this is my string";
$mysecondstring = "thisismystring";
I want to split the last 6 characters from the above strings, here it is "string". How can I do this in PHP?
Using str_split, I can split from the front side, but I need from the end.
Thanks in advance.
Use substr()
edit
$snippet = substr($mystring, 0, -6);
Was looking for something like it too, (almost 10 years later ..)
Here it's what i've made :
function str_split_end($string, $length) {
$string = strrev($string);
$array = str_split($string, $length);
$array = array_reverse($array);
for($i = 0; $i < sizeof($array); $i++) {
$array[$i] = strrev($array[$i]);
}
return $array;
}
In your case, the function will return with $mystring :
array (
0 => 'this ',
1 => 'is my ',
2 => 'string',
)
And with $mysecondstring :
array (
0 => 'th',
1 => 'isismy',
2 => 'string',
)
Using str_split() will Convert a string to an array
But can get the same result output you want in an array form
// The first parameter would the string you want to split.
// then the second parameter would be in what length you want the string to be splitted.
str_split(parameter1,parameter2)
Example.
$mystring = " this is my string ";
// we put length of string to be splitted by 6 because its the string length of "string"
$myString = str_split($myString,6);
It will generate a result of :
Output:
Array
(
[0] => " this "
[1] => "is my "
[2] => "string"
// then you can
echo $string[2]
//for the value of "string" being splitted.
)
note: if your using str_split() to get a value you want you must have a good division length for it. That why i add extra spaces in the value of $mystring to only produce an output array of "string" as value

PHP foreach loop confusion?

So a simple example would be
$ar = array("some text","more text","yet more text");
foreach($ar as $value){
echo $value."<br>";
}
I get the result
some text
more text
yet more text
so my question is when we do this inside foreach loop "$ar as $value",
I know that $ar is array but what about second one the $value is it simple
variable or is it yet another array? Because we can do it in the following way too
foreach($ar as $value){
echo $value[0]."<br>";
}
Which would result in
s
In PHP, strings are byte arrays. Referencing position 0 of $value refers to the position (0) in the string (s in some test)
Your actual array looks like this:
Array
(
[0] => some text
[1] => more text
[2] => yet more text
)
If you want to access the index position of the array you can do:
foreach($ar as $key=>$val) {
echo "$key - $val";
}
Which will output:
0 - some text
1 - more text
2 - yet more text
$value is a value in array and is not an array itself unless you have nested arrays (array(array('a','b'),array('b','c'))). Subscripting strings, though, is still possible and this is how your got the first character of the string.
You should get
s m y
on separate lines.
BTW the br tag is old hat.
The thing is that doing $value[0] access the first character of the string.
A string is internally represented as an array. So accessing to the index 0 of a string is like accessing to the first character.
That is why it prints "s" because your string "some text" starts with s
You can see your example as the following
array(
[0] => array(
[0] => 's',
[1] => 'o',
[2] => 'm',
[3] => 'e',
//...
),
[1] => array(
[0] => 'm',
[1] => 'o',
[2] => 'r',
[3] => 'e',
//...
),
//...
);
String access and modification by character is possible in PHP. What you need to know, and probably didn't know is that while strings are expresses as string, sometimes they can be considered as arrays: let's look at this example:
$text = "The quick brown fox...";
Now, if you were to echo $text[0] you would get the first letter in the string which in this case happens to be T, or if you wanted to modify it, doing $text[0] = "A"; then you will be changing the letter "T" to "A"
Here is a good tutorial from the PHP Manual, It shows you how strings can be accessed/modified by treating them as an array.
<?php
// Get the first character of a string
$str = 'This is a test.';
$first = $str[0];
// Get the third character of a string
$third = $str[2];
// Get the last character of a string.
$str = 'This is still a test.';
$last = $str[strlen($str)-1];
// Modify the last character of a string
$str = 'Look at the sea';
$str[strlen($str)-1] = 'e';
?>
BTW: If you had only wanted to display, the first value inside your array, you could use something like
<?php
$ar = array("some text","more text","yet more text");
for ($i=1; $i<=1; $i++)
{
echo $ar[0];
}
?>

Categories