I'm familiar with some of PHPs string functions, however I can't seem to find the right one to do what I want. What I'm trying to accomplish is to echo 22 characters before and after a string that I find. I'm able to find the starting position of the string as well as the string length...I'm thinking that these might be useful to accomplish what I need to. For example, let's say I find the string "hello". I want to echo "This is an example of hello how are you doing????" Will substr() accomplish this?
$var2 = 'hello';
$startingpos = strpos($var1, $var2));
$strlength = strlen($var2);
UPDATE: I've found the solution to my problem: Please see the below:
$additional_length = 2;
$startingpos = strpos($var1, $var2));
$strlength = strlen($var2);
if (($startingpos - $additional_length) < 0)
$start = 0;
else
$start = $startingpos - $additional_length;
echo substr($var2, $start, ($strlength + (2* $additional_length)))
You can easily set a constant inside your string, and then replace it with str_replace function.
For instance:
$results = str_replace("[FOUND]", "hello", "This is an example of [FOUND] how are you doing");
Working example:
function.onl/str_replace("[FOUND]", "hello", "This is an example of [FOUND] how are you doing");
Demo
Related
I'm working with indexing some news sites. A kind of news clipping.
I'm an amateur and curious. I'm not a programmer so the question may seem silly to anyone in the business. But if anyone can help, thank you.
The paging of the sites I was doing parsing was practically the same and I used this scheme:
$url = $ url. '/page/'. $s;
$next_url = $s + 1;
$prev_url = $s - 1;
if ($prev_url <= 0) {
$prev_url = 1;
}
The format was basically this:
http://example.com/politics/page/2
But yesterday I came across something different and I do not know how to page. I get this link format through preg_match_all:
http://www.example.com/browse-Politics-National-texts-1-date.html
This is the paging part:
-1-
This part is variable:
Political-National-texts
Any guidance?
If what you are asking for is parsing the url for the pagination and variable parts, you can use preg_match with the following regexp:
if (preg_match('/^http:\/\/www.example.com\/browse-([-a-zA-Z]+)-(\d+)-date\.html$/', $url, $matches)) {
var_export($matches);
}
Then you will get the result:
array (
0 => 'http://www.example.com/browse-Politics-National-texts-1-date.html',
1 => 'Politics-National-texts',
2 => '1',
)
The keys in $matches will be:
0: The entire match
1: The first matched group (the variable)
2: The second matched group (the pagination)
<?php
$url = 'http://www.example.com/browse-Politics-National-texts-1-date.html'
$url_basename = basename($url); // extract `browse-Politics-National-texts-1-date.html`
$url_exploded = explode('-',$url_basename); // make an array delimited by `-`
array_pop($url_exploded);
$url_page_number = array_pop($url_exploded); // get the 2nd element from back
?>
Result:
$url_page_number = 1
PS. Could make it shorter, but it's for educational purposes :-)
I want to send some string parameter to a cpp.exe from PHP thanks to exec function. The aim of the exe is to compute a rank of documents according to a query.
Here is the code in php :
$query = "is strong";
$file = "vsm.exe $query";
exec($file,$out);
echo $out[0];`
I received this output for echo $out[0];
Notice: Undefined offset: 0 in C:\xampp\htdocs\analysis.php on line 25
But, my vsm.exe only work (meaning I receive my ranks in the $out variable as a string which is okay) when the query is without space:
$query = "is";
$file = "vsm.exe $query";
exec($file,$out);
echo $out[0];
I followed that example which works with integer parameter (this is not what I want, I want to send sentences):
$a = 2;
$b = 5;
exec("tryphp.exe $a $b",$c_output);
echo $c_output[0];
$c_array0 = explode(" ",$c_output[0]);
echo "Output: " . ($c_array0[0] + 1);
echo "Output: " . ($c_output[0] + 1);
How could I send strings including spaces (could be long text) as parameters to c++?
Thanks in advance.
I actually find something but still not enough good:
$query = "is strong";
$file = 'vsm.exe "is strong"';
exec($file,$out);
echo $out[0];
It returns me the rank I wanted. However, I look for a way to use $query as a parameter, not directly the string "is strong".
Im trying to achieve the following with PHP
sample#gmail.com => s*****#gmail.com
sa#yahoo.com => **#yahoo.com
sampleaddress#hotmail.com => samplead*****#hotmail.com
I want to hide last five characters in the portion that stays before '#'
I can write long code to do this by splitting and then replacing based on lengths, but Im sure there must be an easy way to do this using PHP functions, any help please?
UPDATE:
Im adding my code here, Im sure its not efficient, and thats the reason Im asking it here
$email = 'sampleuser#gmail.com';
$star_string = '';
$expl_set = explode('#',$email);
if(strlen ($expl_set[0]) > 5){$no_stars = 5; }else{$no_stars = strlen ($expl_set[0]); }
for($i=0;$i<$no_stars; $i++)
{
$star_string.='*';
}
$masked_email = substr($expl_set[0], 0, -5).$star_string.'#'.$expl_set[1];
You can wrap it into a function, making it easier to call multiple times.
Basically, split the address and the domain, replace $mask number of characters in the end of the string (default 5) with *, or the length of the address if it's shorter than the amount of masked characters.
function mask_email($email, $masks = 5) {
$array = explode("#", $email);
$string_length = strlen($array[0]);
if ($string_length < $masks)
$masks = $string_length;
$result = substr($array[0], 0, -$masks) . str_repeat('*', $masks);
return $result."#".$array[1];
}
The above would be used like this
echo mask_email("test#test.com")."\n";
echo mask_email("longeremail#test.com");
which would ouput this
****#test.com
longer*****#test.com
You can also specify the number you want filtered by using the second parameter, which is optional.
echo mask_email("longeremail#test.com", 2); // Output: longerema**#test.com
Live demo
I want to write the algorithm for reading character by character from string in PHP. Is it possible to do this without using any builtin function...? If not possible, then can we do this by using minimum function (i.e count/size)....?
A string in PHP is in fact an array and can be addressed as such
$str = 'Hello World';
echo $str[0]; // H
echo $str[1]; // e
yes for example the string is "Hello world" then here is the code to access it
<?php
$var = "Hello World!!" ;
for($count = 0 ; $count < strlen($var) ; $count ++)
{
echo $var[$count] ;
}
?>
The above code traverses the string linearly till the value of counter is less than the string length owing to the fact that the counter was initialised to 0, Hope it helps.
This question already has answers here:
Closed 10 years ago.
How can I compare the two large strings of size 50Kb each using php. I want to highlight the differentiating bits.
Differences between two strings can also be found using XOR:
$s = 'the sky is falling';
$t = 'the pie is failing';
$d = $s ^ $t;
echo $s, "\n";
for ($i = 0, $n = strlen($d); $i != $n; ++$i) {
echo $d[$i] === "\0" ? ' ' : '#';
}
echo "\n$t\n";
Output:
the sky is falling
### #
the pie is failing
The XOR operation will result in a string that has '\0' where both strings are the same and something not '\0' if they're different. It won't be faster than just comparing both strings character by character, but it'd be useful if you want to just know the first character that's different by using strspn().
Do you want to output like diff?
Perhaps this is what you want https://github.com/paulgb/simplediff/blob/5bfe1d2a8f967c7901ace50f04ac2d9308ed3169/simplediff.php
ADDED:
Or if you want to highlight every character that is different, you can use a PHP script like this:
for($i=0;$i<strlen($string1);$i++){
if($string1[$i]!=$string2[$i]){
echo "Char $i is different ({$string1[$i]}!={$string2[$i]}<br />\n";
}
}
Perhaps if you can tell us in detail how you would like to compare, or give us some examples, it would be easier for us to decide the answer.
A little modification to #Alvin's script:
I tested it in my local server with a 50kb lorem ipsum string, i substituted all "a" to "4" and it highlight them. It runs pretty fast
<?php
$string1 = "This is a sample text to test a script to highlight the differences between 2 strings, so the second string will be slightly different";
$string2 = "This is 2 s4mple text to test a scr1pt to highlight the differences between 2 strings, so the first string will be slightly different";
for($i=0;$i<strlen($string1);$i++){
if($string1[$i]!=$string2[$i]){
$string3[$i] = "<mark>{$string1[$i]}</mark>";
$string4[$i] = "<mark>{$string2[$i]}</mark>";
}
else {
$string3[$i] = "{$string1[$i]}";
$string4[$i] = "{$string2[$i]}";
}
}
$string3 = implode("",$string3);
$string4 = implode("",$string4);
echo "$string3". "<br />". $string4;
?>