I have been working on a script that pulls information from a certain website. The said website pulls the information from a database and displays it in a way the user can easily read it (like always).
Imagine it looks like this:
Var1: result1
Var2: result2
Var3: result3
What my script does is that it reads the page's source code and retrieves "result1", "result2" and "result3" by obtaining the text between two strings.
Sample code:
<?php
function get_string_between($string, $start, $end) {
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
function check($url) {
// usually, $fullstring = file_get_contents($url);
$fullstring = "<string1>result1</string1><string1>result2</string1><string1>result3</string1>";
$result = get_string_between($fullstring, "<string1>", "</string1>");
echo "<b>Result: </b>".$result;
}
check("random"); // just to execute the function
?>
In case you wonder why I have the check() function there it is because this code is part of something bigger and I need a solution that works in this case scenario, so I tried to keep it immaculate.
Now, I can easily get "result1" because it's the first occurrence, but how can I get "result2" and "result3"?
Thank you :)
Use a regex to extract all of the matches, then pick the ones you want:
function get_string_between($string, $start, $end)
{
preg_match_all( '/' . preg_quote( $start, '/') . '(.*?)' . preg_quote( $end, '/') . '/', $string, $matches);
return $matches[1];
}
The regex will capture anything between the $start and $end variables.
Now the function returns an array of all of the result values, which you can pick which one you want:
list( $first, $second, $third) = get_string_between( $string, "<string1>", "</string1>");
You can see it working in this demo.
Related
So I have this practical page I made to see if I can make a template language, the code is listed below:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
function get_attributes($element) {
$output = explode(" ", $element);
return $output;
}
function build_element($item, $attributes) {
switch($element) {
case "form";
$template = "<form {{attributes}}>";
$template = str_replace("{{attributes}}", $attributes, $template);
return $template;
break;
}
}
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$fullstring = '
<xe:form style="width:100px; height: 100px; background: #55ff55;"></xe:form>
';
$parsed = get_string_between($fullstring, '<xe:', '>');
$e = get_attributes($parsed);
$full = '<{{et}} {{attr}}></form>';
$full = str_replace('{{attr}}', $e[1], $full);
// the str_replace() bellow this comment is causing issues
$full = str_replace('{{et}}', $e[0], $full); // <--------------------- issue is here
echo $full;
It seems like if I add 2 str_replace functions, the echo is blank, and the $e var is working fine.
I tried echoing out both $e vars, but they are both fine.
If someone could point me in the right direction, I'd greatly appreciate it.
The result is:
<form style="width:100px;></form>
Not sure what do you want exactly, but:
the style attribute is opened with a double quote, but not closed, causing the form to be not visible.
you didn't parse the attributes perfectly as you miss the height and bgcolor. (separating with the space is not a good idea as some attributes can have space in the value)
HTML is fair complex, you might want to check out https://www.php.net/manual/en/class.domdocument.php to manipulate it without weird issues. You may change a few things with search&replace, but they will break easily.
I Am working on a project that requires me to find all of the functions across several hundreds of php files, and return the line number that the function was first created on, as well as the line number that the function closes on. The entire file is loaded in as a string, and each line within the file begins with a line number. I have found the number that the functions start on, and have in variables the function name, meaning that i can establish a pattern, but cant seem to narrow down a way to figure out where it ends.
A portion of the string may look something like this:
Line11 blah blah blah Line12 function doSomething ( $foo, $fab ) Line13 { Line14 ... Line15 { Line16 ... Line18 ... Line19 ... Line21 if (... ) Line22 ... ( ... ); Line23 break; Line24 } Line25 die ( ... ); Line26 } Line27 Blah Blah Blah
In this case, I would need to know that the function ends on line 26.
Ive tried using a function to get the number by matching reliable patterns on each side of it:
function for finding strings between patterns:
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$name = 'doSomething';
$paradigm = '$foo, $fab';
$start = 12;
$mess = 'Line'.$start.'function'.$name.'('.$paradigm.')'.'/\{([^{}]|(?R))*\}/'.' Line';
$lineNum = get_string_between($string, $mess, ' ');
echo $lineNum;
I was expecting an output of 26, but currently, it doesn't return anything.
I am trying to check if a string contains something between {} and if it does replace it with what it relates too the issue I am having with this is calling it to itself
For reference lets assume we have a string https://technologyforthefuture.org/open-doors-challenge/?modalActive=true&video_id={user_video_id}&key-={user_affiliate_id}
We pass this string into the below function ParseShortcodes()
What will happen currently is {user_video_id} will be noticed and replaced and then that will be the end of the loop and nothing will be returned because the code is waiting for $short to be empty to return template
I had though about calling ParseShortcodes() within itself but I dont think this is the right way to do this there must be some better way
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
function ParseShortcodes($template,$post_id){
$short = get_string_between($template,"{","}");
if($short == "user_affiliate_id"){
global $wpdb;
$query = $wpdb->get_row("SELECT * FROM wp_uap_affiliates WHERE uid=3");
$short = $query->id;
$template = str_replace("{user_affiliate_id}",$short,$template);
}else if($short == "user_video_id"){
$template = str_replace("{user_video_id}",$post_id,$template);
}else if(empty($short)){
return $template;
}
}
Just use preg_replace_callback to look up all {words} and pass it to your callback:
$templ = preg_replace_callback("/\{(\w+)\}/", "DoShortCodes", $templ);
That will invoke the function, passing only user_video_id (or whatever), and replacing it in the source string with its substitute. Thus the callback can be trimmed down to:
function DoShortCodes($m) {
switch($m[0]) {
case "vidid": return "0";
case "userthing": return db("SELECT ? as id", $x)->id;
default: trigger_error("NO FINDY SHORTCODE");
}
}
Which obsoletes the replacement step.
how to extract specific string after specific word using html dom in php. I have
<div class="abc">
<script type="text/javascript">
var flashvars = { word : path } </script>
Now i want to extract path after word
thanks for your response.
I got the solution for what i was looking.
Here is the code in case someone needs it.
Explanation :
'$results' is the curl response.
Enter div class name (which you want to fetch) inside "$xpath->query() function"
You will get source code for entire class inside "$tag->textContent"
$dom = new DOMDocument();
$dom->loadHTML($results);
$xpath = new DOMXPath($dom);
$tags = $xpath->query('//div[#class="e"]');
foreach ($tags as $tag)
{
echo "<br>----------<br>";
var_dump($tag->textContent);
echo "<br>----------<br>";
}
Now you have your required class' html source inside "$tag->textContent".
Now you can fetch anything from the string between "start" and "end" points using below function.
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
In my case i used it like this :
$price = get_string_between($tag->textContent,'swf', '+');
echo $price;
Here "swf" is the starting point of the path and "+" is the end point.
Hope it saves somebody else time :)
I am having a file like this and I am trying to replace the file:
abc.txt
# Define the right-hand side of the equation:
#{xvalue1#}xval1= ;#xvalue1
#{xvalue2#}xval2= ;#xvalue2
What I did is as follows:
$myfile = fopen("abc.txt", "r") or die("Unable to open file!");
$data = fread($myfile,filesize($pgm_file));
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
$parsed0 = get_string_between($data, "#{xvalue1#}xval1=", ";#xvalue1");
$parsed1 = get_string_between($data, "#{xvalue2#}xval2=",";#xvalue2");
I am trying to replace the values as follows:
$datanew0 = str_replace($parsed0,"5", $data);
$datanew1 = str_replace($parsed1,"10", $datanew0);
When I echoed echo $datanew1;
I am getting output as :
# Define the right-hand side of the equation:
#{xvalue1#}xval1= 5;#xvalue1
#{xvalue2#}xval2= 5;#xvalue2
My expected result is:
# Define the right-hand side of the equation:
#{xvalue1#}xval1= 5;#xvalue1
#{xvalue2#}xval2= 10;#xvalue2
I'd recommend to rather work with the positions within the string than extracting and replacing stuff.
Take a look at this example
function injectBetween($what, $start, $end, $src){
$lpos = strpos($src, $start);
$rpos = strrpos($src, $end);
return substr($src, 0, $lpos + strlen($start)) . $what . substr($src, $rpos);
}
var_dump(injectBetween('test', 'start', 'end', 'startend'));
Will give you string 'starttestend' (length=12)
Or to match your example:
var_dump(injectBetween('5', '#{xvalue1#}xval1= ', ';#xvalue1', '#{xvalue1#}xval1= ;#xvalue1'));
I don't know how your full input looks like. If you have multiple lines that have the same pattern this will most probably fail. Better use regular expressions in that case or parse your input line by line (using explode()).
Why not using RegEx ? it is more appropriate, efficient and maintainable implementation :
<?php
$subject = "#{xvalue1#}xval1= 19;#xvalue1";
$pattern = "/#\{xvalue[0-9]+#\}xval[0-9]+= ([0-9]+);#xvalue[0-9]+/";
preg_match_all($pattern, $subject, $matchs);
var_dump($matchs);
EDIT:
I suggest you to change your pattern like this :
#{xvalue#}xval1= ;#xvalue
And the RegEx pattern will be :
$pattern = "/#\{xvalue#\}xval([0-9]+)= ([0-9]+);#xvalue/";
Note that i have also captured the key associated to your value