PHP str_replace notworking if used 2 times - php

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.

Related

Finding a pattern in a PHP file and reading the number after that pattern

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.

Recursive function to call itself untill it returns a value

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.

Extract string using htmldom php

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 :)

PHP - preg_replace multiple patterns, while keeping what's inbetween two other patterns

I've recently started using regex functions to simplify what I previously had. I'm attempting to turn this,
Some text
'[img="http://www.example.com/null.jpg"]'
Some more text
into this:
Some text
<img src="http://www.example.com/null.jpg">
Some more text
Is this possible in php using regex functions (such as 'preg_replace' and 'preg_match')?
***EDIT[
Here is my current clunky function
function filterimgorig($string)
{
if(substr_count($string, '[img="')==substr_count($string, '"]') and substr_count($string, '[img="')+substr_count($string, '"]')!=0)
{
$start = strpos($string, '[img="')."<br/>";
$end = strpos($string, '"]');
$img = substr($string, $start+6, ($end - $start)-6);
if (filter_var($img, FILTER_VALIDATE_URL) === false) {
return htmlspecialchars(str_replace(''',"'",$string));
}
else
{
return substr($string, 0, $start)."\r\n<img src='".$img."'>\r\n".substr($string, $end+2,strlen($string));
}
}
else
{
return htmlspecialchars(str_replace(''',"'",$string));
}
}
You could do:
$string = preg_replace("/'\[img=([^]]+)\]/", "<img src=$1>", $string);

How to find the second string between two strings

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.

Categories