PHP character remove in string - php

hello i have a variable that has the text below:
<p>Not sure exactly which property you want to extract, but I assume it's the 'question_answers_url'.</p>
<pre><code>$answersArray = Array();
for($i=0;$i<count($jsonArray['questions']);$i++){
//assuming it is the 'question_answers_url' property that you want
array_push($answersArray,$jsonArray['questions'][$i]['question_answers_url']);
}
</code></pre>
<p>Ought to do it.</p>
how can i get only the text that is between <p>TEXT_I_WANT</p> and trash everything else.

preg_match_all("/<p>(.*?)<\/p>/is",$text,$matches,PREG_SET_ORDER);
foreach($maches as $match){
echo $match[1];
}
Untested !!
Updated!

Related

PHP - Replace everything between String including content

I want to iterate over a string which looks like this.
Booo ahh [Hello] and what is [Baby] at your [Mouse]
My target is to replace every [###] with another string, which is not static, but can handle the content between the brackets.
Should be something like
function replace_string($text) {
...
//Maybe some kind of loop for all brackets
{
$content = ... //For example Hello, Baby, Mouse => ###
$replacement = "I'm a " . $content . "!";
...
}
return $replacedString;
}
I don't think it can work like in my "suggestion", but I hope I could show what I want to do and somebody can help me.
For what i understand, you want to have this string at the end:
Booo ahh [I'm a Hello!] and what is [I'm a Baby!] at your [I'm a Mouse!]
So you can use preg_replace function: http://php.net/manual/en/function.preg-replace.php
Your code will looks like:
function replace_string($text)
{
return preg_replace('/\[([^\]]+)\]+/', "[I'm a $1!]", $string);
}
so
<?php
$string = 'Booo ahh [Hello] and what is [Baby] at your [Mouse]';
echo replace_string($string);
// will display
// Booo ahh [I'm a Hello!] and what is [I'm a Baby!] at your [I'm a Mouse!]
not sure if you want to keep the brackets or not, if you don't, and want to have
Booo ahh I'm a Hello! and what is I'm a Baby! at your I'm a Mouse!
just use:
preg_replace('/\[([^\]]+)\]+/', "I'm a $1!", $string);
You can see the regex here: https://regex101.com/r/8hJQr0/1

PHP regex templateparser

Dear programmers and code freaks,
I've got this function
<?php
function ListRow($name) {
preg_match_all("'{row name ='".$name."'}(.*?){\/row}'si", $this->tpl, $match);
return $match[1];
}
?>
What it is supposed to do is getting the info between {row name = 'products'} and {/row}.
It does get the data between the tags if they're on the same line, but with enters between
them, it doesn't capture anything. I'm kinda stuck in this one so i would appreciate some help
more then anything.
This works, for me (see the "return" line, it's the only difference with your code...):
<?php
print ListRow("abc");
function ListRow($name) {
preg_match_all("/{row name ='".$name."'(.*?){\/row}/si", "{row name ='abc'
TEXT_SEARCHED
{/row}", $match);
return $match[1][0];
}
?>
And now, let's test it...:
$ php test.php
TEXT_SEARCHED

Additional elements to URLS?

I'm not sure what the terminology is, but basically I have a site that uses the "tag-it" system, currently you can click on the tags and it takes the user to
topics.php?tags=example
My question is what sort of scripting or coding would be required to be able to add additional links?
topics.php?tags=example&tags=example2
or
topics.php?tags=example+example2
Here is the code in how my site is linked to tags.
header("Location: topics.php?tags={$t}");
or
<?php echo strtolower($fetch_name->tags);?>
Thanks for any hints or tips.
You cannot really pass tags two times as a GET parameter although you can pass it as an array
topics.php?tags[]=example&tags[]=example2
Assuming this is what you want try
$string = "topics.php?";
foreach($tags as $t)
{
$string .= "tag[]=$t&";
}
$string = substr($string, 0, -1);
We iterate through the array concatenating value to our $string. The last line removes an extra & symbol that will appear after the last iteration
There is also another option that looks a bit more dirty but might be better depending on your needs
$string = "topics.php?tag[]=" . implode($tags, "&tag[]=");
Note Just make sure the tags array is not empty
topics.php?tags=example&tags=example2
will break in the back end;
you have to assign the data to one variable:
topics.php?tags=example+example2
looks good you can access it in the back end explode it by the + sign:
//toplics.php
<?php
...
$tags = urlencode($_GET['tags']);
$tags_arr = explode('+', $tags); // array of all tags
$current_tags = ""; //make this accessible in the view;
if($tags){
$current_tags = $tags ."+";
}
//show your data
?>
Edit:
you can create the fron-end tags:
<a href="topics.php?tags=<?php echo $current_tags ;?>horror">
horror
</a>

Stripping text from url

Im trying to strip find_loc= and &cflt=pizza I got the majority stripped its just these last 2 things and whenever I try to use trim it doesn't delete it out it keeps saying array even when i try to print it, it says array.
<?php
$foo = 'http://www.yelp.com/search?find_loc=2190+W+Washington+Blvd%2C+Los+Angeles+90018&cflt=pizza ';
$blah = parse_url($foo);
$blah[query];
//the code above echos out find_loc=2190+W+Washington+Blvd%2C+Los+Angeles+90018&cflt=pizza
$thids = trim(''.$blah.'','find_loc=');
echo $thids;
?>
$thids = str_replace(array('&cflt=pizza','find_loc='), '', $blah);
parse_str($blah['query'], $query_vars); // decompose query string into components
unset($query_vars['find_loc']); // delete this particular query variable/value
unset($query_vars['cflt']);
$blah['query'] = http_build_query($query_vars); // rebuild the query string
$foo = http_build_url($blah); // rebuild the url

identify and execute php code on a string

I would like to know if it's possible to execute the php code in a string. I mean if I have:
$string = If i say <?php echo 'lala';?> I wanna get "<?php echo 'dada'; ?>";
Does anybody knows how?
[EDIT] It looks like nobody understood. I wanna save a string like
$string = If i say <?php count(array('lala'));?>
in a database and then render it. I can do it using
function render_php($string){
ob_start();
eval('?>' . $string);
$string = ob_get_contents();
ob_end_clean();
return $string;
}
The problem is that I does not reconize php code into "" (quotes) like
I say "<?php echo 'dada'; ?>"
$string = ($test === TRUE) ? 'lala' : 'falala';
There are lots of ways to do what it looks like you're trying to do (if I'm reading what you wrote correctly). The above is a ternary. If the condition evaluates to true then $string will be set to 'lala' else set to 'falala'.
If you're literally asking what you wrote, then use the eval() function. It takes a passed string and executes it as if it were php code. Don't include the <?php ?> tags.
function dropAllTables() {
// drop all tables in db
}
$string = 'dropAllTables();';
eval($string); // will execute the dropAllTables() function
[edit]
You can use the following regular expression to find all the php code:
preg_match_all('/(<\?php )(.+?)( \?>)/', $string, $php_code, PREG_OFFSET_CAPTURE);
$php_code will be an array where $php_code[0] will return an array of all the matches with the code + <?php ?> tags. $php_code[2] will be an array with just the code to execute.
So,
$string = "array has <?php count(array('lala')); ?> 1 member <?php count(array('falala')); ?>";
preg_match_all('/(<\?php )(.+?)( \?>)/', $string, $php_code, PREG_OFFSET_CAPTURE);
echo $php_code[0][0][0]; // <?php count(array('lala')); ?>
echo $php_code[2][0][0]; // count(array('lala'));
This should be helpful for what you want to do.
Looks like you are trying to concatenate. Use the concatenation operator "."
$string = "if i say " . $lala . " I wanna get " . $dada;
or
$string = "if i say {$lala} I wanna get {$dada}.";
That is what I get since your string looks to be a php variable.
EDIT:
<?php ?> is used when you want to tell the PHP interpreter that the code in those brackets should be interpreted as PHP. When working within those PHP brackets you do not need to include them again. So as you would just do this:
// You create a string:
$myString = "This is my string.";
// You decide you want to add something to it.
$myString .= getMyNameFunction(); // not $myString .= <?php getMyNameFunction() ?>;
The string is created, then the results of getMyNameFunction() are appended to it. Now if you declared the $myString variable at the top of your page, and wanted to use it later you would do this:
<span id="myString"><?php echo $myString; ?></span>
This would tell the interpreter to add the contents of the $myString variable between the tags.
Use token_get_all() on the string, then look for a T_OPEN_TAG token, start copying from there, look for a T_CLOSE_TAG token and stop there. The string between the token next to T_OPEN_TAG and until the token right before T_CLOSE_TAG is your PHP code.
This is fast and cannot fail, since it uses PHP's tokenizer to parse the string. You will always find the bits of PHP code inside the string, even if the string contains comments or other strings which might contain ?> or any other related substrings that will confuse regular expressions or a hand-written, slow, pure PHP parser.
I would consider not storing your PHP code blocks in a database and evaluating them using eval. There is usually a better solution. Read about Design Pattern, OOP, Polymorphism.
You could use the eval() function.

Categories