php regexp: Find form with hidden input & add another input - php

i need help with this problem:
I have a HTML string with output HTML code and i need search all forms where is specific input (hidden with given name and value). After this hidden input i need add another input with given params (only value is dynamically).
Thanks for replies!
// Sorry for my bad english...

You could do something like this;
<?php
$html = file_get_contents('form.html');
if (preg_match('/\<.+? name="other" .+?\>/', $html, $match)) {
$element = $match[0];
$element .= '<input type="hidden" name="appended" value="etry">';
$html = str_replace($match[0], $element, $html);
}
echo $html;
In this example it will look for a element that starts with '<' and contains 'name="other"', and ends with '>'. Then it uses this to replace itself in the original document, and append itself again, with the additional html.
This is just an example, as it does not check if you have multiple matches etc.

Related

How can I insert text in an html element stored in a variable?

I have a program which is copying the text from another website and showing it.
It is storing the text in a variable $string.
The variable is containing html tags in it and I want to add text before a html tag stored in the variable.
For example: $string="<div id='1'><div id='game'></div>"; I want to add text before the div whose id is game.
To add the text before the div whose id is 'game'. simply use:
$string = "<div id='1'><div id='game'></div>";
$new = "texttoinsert";
$pos = "<div id='game'></div>";
echo str_replace($pos, $new.$pos ,$string);
In php the easiest way to do this would be using str_replace (http://www.php.net/manual/en/function.str-replace.php).
$textToInsert = "test";
$string = str_replace("<div id='game'>", $textToInsert."<div id='game'>" ,$string);
For that particular case the following works:
$($string).prepend("text");
DEMO
Using jQuery the solution is simple:
var text = $("<div id='1'><div id='game'></div></div>");
$('#1', text).prepend('text-to-insert');
and the result HTML can be obtained like this: text.html()
I hope this help.

How can I remove multiple "[" in HTML Tag ID attribute with Regex?

How can I remove multiple "[" in HTML Tag ID attribute with Regex?
For example
<div id="test[12][45][67]">test inline [for example]</div>
Change to
<div id="test12]45]67]">test inline [for example]</div>
What you could (most likely should) do is to use a PHP HTML parser, such as PHP Simple DOM Parser to go over the HTML, extract the ID value and then replace it using something like so:
<?php
$string = <yourID>
$pattern = '/\[/g';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>
This would allow you to safely modify only your id section in a relatively simple manner.
using javascript:
function rem_open_square_brackets(_id) {
if(_id) {
return _id.replace(/[\[]*/g, '');
}
return _id;
}
var _id = rem_open_square_brackets('test[12][45][67]');
document.write(_id);
you could find all your ids and past them to the function and replace them with the return value.

how to remove links from a html content using php

I have the following html content:
<p>My name is way2project</p>
Now I want this text as <p>My name is way2project</p>
Is there any way to do this? Please help me thanks
I used preg_replace but in vain.
Thanks again
You can use the strip tags function
$string = '<p>My name is way2project</p>';
echo strip_tags($string,'<p>');
note the second parameter is the list of allowed tags you wont to ignore.
This seems strange, but not knowing the complete scope of your issue and seeing that you want to do this in PHP, you can try:
$origstring = '<p>My name is way2project</p>';
$newstring = str_replace('way2project', 'way2project', $origstring);
echo $newstring;
Checkout Simple Html Dom Parser
$html = str_get_html('<html><body>Hello!SO</body></html>');
echo $html->find('a',0)->innertext; //prints "SO"
strip_tags you can use this, to remove html tags.

create anchors in a page with the content of <h2></h2> in PHP

Well I have a html text string in a variable:
$html = "<h1>title</h1><h2>subtitle 1</h2> <h2>subtitle 2</h2>";
so I want to create anchors in each subtitle that has with the same name and then print the html code to browser and also get the subtitles as an array.
I think is using regex.. please help.
I think this will do the trick for you:
$pattern = "|<h2>(.*)</h2>|U";
preg_match_all($pattern,$html,$matches);
foreach($matches[1] as $match)
$html = str_replace($match, "<a name='".$match."' />".$match, $html);
$array_of_elements = $matches[1];
Just make sure that $html has the existing html before this code starts. Then it will have an <a name='foo' /> added after this completes, and $array_of_elements will have the array of matching text values.

Highlight Search Terms in PHP without breaking anchor tags using regex

I'm searching through some database search results on a website & trying to highlight the term in the returned results that matches the searched term. Below is what I have so far (in php):
$highlight = trim($highlight);
if(preg_match('|\b(' . $highlight . ')\b|i', $str_content))
{
$str_content = preg_replace('|\b(' . $highlight. ')(?!["\'])|i', "<span class=\"highlight\">$1</span>",
$str_break;
}
The downside of going this route is that if my search term shows up in the url permalink as well, the returned result will insert the span into the href attribute and break the anchor tag. Is there anyway in my regex to exclude "any" information from the search results that appear in between an opening and closing HTML tag?
I know I could use the strip_tags() function and just spit out the results in plain text, but I'd rather not do that if I didn't have to.
DO NOT try to parse HTML with regular expressions:
RegEx match open tags except XHTML self-contained tags
Try something like PHP Simple HTML DOM.
<?php
// get DOM
$html = file_get_html('http://www.google.com/search?q=hello+kitty');
// ensure this is properly sanitized.
$term = trim($term);
// highlight $term in all <div class="result">...</div> elements
foreach($html->find('div.result') as $e){
echo str_replace($term, '<span class="highlight">'.$term.'</span>', $e->plaintext);
}
?>
Note: this is not an exact solution because I don't know what your HTML looks like, but this should put you pretty close to being on track.
I think assertions is what your looking for.
I ended up going this route, which so far, works well for this specific situation.
<?php
if(preg_match('|\b(' . $term . ')\b|i', $str_content))
{
$str_content = strip_tags($str_content);
$str_content = preg_replace('|\b(' . $term . ')(?!["\'])|i', "<span class=\"highlight\">$1</span>", $str_content);
$str_content = preg_replace('|\n[^<]+|', '</p><p>', $str_content);
break;
}
?>
It's still html encoded, but it's easier to parse through now without html tags

Categories