php string convert to html - php

I have a string <div id="myid">...</div>
How would I change this into
<div id="myid">...</div>
I have tried a few things but no luck any help?
Update
function get_page(){
$file = 'file.php';
$str = file_get_contents($file);
$str = html_entity_decode($str);
return $str;
}
$output = get_page();
echo $output;//don't work
FIX
function get_page(){
$file = 'file.php';
$str = file_get_contents($file);
return $str;
}
$output = get_page();
echo html_entity_decode($output);//works

The function for that is htmlspecialchars_decode().
Note that for the function to decode quotes, you need to specify the $quote_style parameter.

html_entity_decode is what you need: http://www.php.net/manual/en/function.html-entity-decode.php

$from = array('<', '>');
$to = array('<', '>');
$string = str_replace($from, $to, $string);

use this it's better ...
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>

htmlspecialchars_decode($string)

Related

Is there a way to display the text content of a field in PHP?

So I have two fields in s template page:
<?php echo get_post_field('post_content', 12345); ?>
<?php the_field('advertisement_one', 12345); ?>
The first field renders as:
<p>
<img src="test.jpg">
</p>
The second field renders as:
<p>
<img src="test2.jpg">
</p>
Is there a way that I can just pull the text content from the "src" in both fields?
My goal is to write/display:
test.jpg
or
test2.jpg
You can try this:
<?php
$str = get_post_field('post_content', 12345);
$str = str_replace("\n", '', $str);
$str = str_replace('<p><img src="', '', $str);
$str = str_replace('"></p>', '', $str);
echo $str;
?>
$content1 = get_post_field('post_content', 12345);
$content2 = get_field('advertisement_one', 12345);
$src1 = preg_replace('|^.+src=["\'](.*)["\'].+$|s','$1',$conten1);
$src2 = preg_replace('|^.+src=["\'](.*)["\'].+$|s','$1',$conten2);
You can use preg_match to check img tag and fetch its src value
please try this code
<?php $value = get_post_field('post_content', 12345);
$value2 = get_field('advertisement_one', 12345);
$src1 = preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $value, $matches);
echo $matches[1];
$src2 = preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $value2, $matches);
echo $matches[1];
?>

how to cut tag in string with PHP

I Have a problem with this output receive value.
$simple="<TRAN_ID>17564_36428.1354_4159</TRAN_ID>
<TRAN_DATE>20160201</TRAN_DATE>
<TRAN_TIME>10:07:08</TRAN_TIME>
<ERROR_CODE>1</ERROR_CODE>
<ERROR_DESC>Not Input Policy</ERROR_DESC>
<POLICY_NBR></POLICY_NBR>";
I want to cut the code with PHP.
TRAN_ID = ?
TRAND_DATE = ?
ERROR_CODE = ?
ERROR_DESC = ?
How can i do it. sorry my english is bad.
Thanks.
You can use PHP's SimpleXML library, like so:
<?php
$str ="<TRANS><TRAN_ID>17564_36428.1354_4159</TRAN_ID><TRAN_DATE>20160201</TRAN_DATE><TRAN_TIME>10:07:08</TRAN_TIME><ERROR_CODE>1</ERROR_CODE><ERROR_DESC>Not Input Policy</ERROR_DESC><POLICY_NBR></POLICY_NBR></TRANS>";
$transaction = simplexml_load_string($str);
echo $transaction->TRAN_ID.PHP_EOL;
echo $transaction->TRAN_DATE.PHP_EOL;
echo $transaction->TRAN_TIME.PHP_EOL;
echo $transaction->ERROR_CODE.PHP_EOL;
echo $transaction->ERROR_DESC.PHP_EOL;
echo $transaction->POLICY_NBR.PHP_EOL;
Note that I added <TRANS> start and end tags to your string.
If the data always looks like the sample, this should work okay.
<?php
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
$str = '<textformat leading="2"><p align="left"><font size="10">get me</font></p></textformat>';
$txt = getTextBetweenTags($str, "font");
echo $txt;
?>

Replace string between two char(special symbol) in php

I have a string like: He *is* a good boy. How *are* you. Then I want to replace is and are with input type textbox means replace things between asterisk(*). How can I get this Please help me out.
<?php
$buffer = 'He *is* a good boy. How *are* you.';
echo "Before: $buffer<br />";
$buffer = preg_replace_callback('/(\*(.*?)\*)/s', 'compute_replacement', $buffer);
echo "After: $buffer<br />";
function compute_replacement($groups) {
// $groups[1]: *item*
// $groups[2]: item
return '<input type="text" value="'.$groups[2].'" />';
}
?>
The result:
try this,
<?php
$x="hai, *was/is* are you, is this *was* test ";
echo preg_replace("/\*[\w\/]*\*/","",$x);
?>
Use preg_replace(); e.g:
<?php
$pattern = '/\*\w+\*/';
$string = 'he *is* a good boy';
$replacement = 'was';
echo preg_replace($pattern, $replacement, $string);
Yields:
he was a good boy
Try in this way:
$txt = "He *is* a good boy. How *are* you.";
$_GET['one'] = "doesn't";
$_GET['two'] = "think about";
preg_match_all( '{\*[^*]+\*}',$txt,$matches );
$txt = str_replace( $matches[0][0], $_GET['one'], $txt );
$txt = str_replace( $matches[0][1], $_GET['two'], $txt );
echo $txt;
3v4l.org demo
or, with preg_replace, in this way:
$txt = preg_replace
(
'/^(.*)\*[^*]+\*(.*)\*[^*]+\*(.*)$/', # <-- (Edited)
"\\1{$_GET[one]}\\2{$_GET[two]}\\3",
$txt
);

Convert space seperated string to class in php

I have a string 'abc back'
Is there a simple way to convert this to like this "AbcBack" in PHP
Just try with ucwords:
$input = 'abc back';
$output = str_replace(' ', '', ucwords($input));
Use ucfirst() function in php.Use the code below
<?php
$string='abc back';
$p = explode($string," ");
$text="";
foreach($p as $m){
$text .= ucfirst($m);
}
echo str_replace(" ","",$text);// will print AbcBack
You can use ucwords() to. Use the code below
<?php
$string='abc back';
echo str_replace(" ","",ucwords($string));
Hope this helps you

php replace paragraph to newline

how to replace <p>hello</p> <p>world</p> to hello<br />world <br />
I've tried searching on stack but there is no matched result.
You could do this by using str_replace() function.
For instance:
$string = "<p>hello</p> <p>world</p>";
$string = str_replace('<p>', '', $string);
$string = str_replace('</p>', '<br />' , $string);
I try this myself and get what I expected
$pattern = '/<p>(\w+)\<\/p>/';
$subject = '<p>hello</p><p>world</p>';
$replacement = '${1}<br/>';
$out = preg_replace($pattern, $replacement, $subject);
I just wonder which is better regex or str_replace
I wrote a better solution, hope everybody can see it helpful and maybe improve it
$pattern = '/<p(.*?)>((.*?)+)\<\/p>/';
$replacement = '${2}<br/>';
$subject = 'html string';
$out = preg_replace($pattern, $replacement, $subject);
Use this to prevent breaking the first and last <p></p> :
$string = str_replace($string, '</p><p>', '');
But if a space comes between the tags, it won't work.
<?php
$str = '<p>hello</p> <p>world</p>';
$replaceArr = array('<p>', '</p>', '</p> <p>');
$replacementArr = array('', '', '<br />');
$str = str_replace($replaceArr, $replacementArr, $str);
echo $str;
?>
Try above code.

Categories