I want to replace strings of text and html on a webpage. This is for the purpose of highlighting pieces of text. So, highlighted text is stored in a database, and when its webpage is loaded, the code should find and replace the piece of text.
For example,
$original_string = "SOME TEXT";
$replacement_string = "<span data-id='7334' class='highlight'>SOME TEXT</span>";
I had issues using PHP str_replace, as it wasn't very good with replacing weird html entities or larger strings.
So now, I am trying to do it on the client-side using JS, and have had much better results using js replace().
My question is - what is the best way to achieve the above goal? Should I use a PHP parser or stick with JS ?
And if I use JS, how should I structure the for loop so that all replacements are made on the same text? This did not quite work:
var value = $('#domainContainer').html();
var $highlightsArray_search = <?php echo json_encode($highlightsArray_search); ?>;
var $highlightsArray_replace = <?php echo json_encode($highlightsArray_replace); ?>;
for(var i=0; i<2; i++){
var search_value = $highlightsArray_search[i];
var replace_value = $highlightsArray_replace[i];
var formattedSnippet = value.replace(search_value, replace_value);
value = formattedSnippet;
}
$('#domainContainer').html(formattedSnippet);
You have to wrap php code with quotes .Just change like this,
var $highlightsArray_search = "<?php echo json_encode($highlightsArray_search); ?>;"
^ ^
var $highlightsArray_replace = "<?php echo json_encode($highlightsArray_replace); ?>;"
^ ^
Take your html code in to php string for example.
<?php
$original_string = "SOME TEXT";
$first ="<span data-id='7334' class='highlight'>";
$second = "</span>";
echo $first.$original_string.$second;
?>
Try this may be helpful
Related
I have a text area field and I want to get value as variable so that I can use these variable to fetch data from mysql.
<?
$sms = $_POST['message'];
$whatIWant = substr($sms, strpos($sms, "#") + 1);
echo $whatIWant;
?>
this is a image of textarea
I tried these code but it's not output as I want.
How can I retrieve variables from this input value? Thanks
You can try something like:
$str = 'Hello #Fahim#. Im from #Mazegeek,Inc#. My phone no is #011111#';
preg_match_all('/(?<=#).*?(?=#)/', $str, $m);
print_r($m);
let me know if it works.
I have a very ugly piece of code in php that does something like this:
for ($i = 0; $i <= ($fi_int-1); $i++) {
$song_uri = urlencode($songs[$i]);
echo "URI: " . $song_uri . "<br>";
echo "<li><a href='#' onclick='PlaySong(".$song_uri.")'>" . $songs[$i] . "</li><br>";
}
Now when PlaySong() is being called, this happens:
<script>
function PlaySong(title) {
title = decodeURIcomponent(title));
document.getElementById("player").src = "./mp3/" + title;
}
</script>
The current problem is when $songs[$i] is being passed to PlaySong(), it looks like that right away:
PlaySong(Name+Of+The+Song+-+Artist+LastName+blah) {
...
}
So JS obviously has a problem with it. It tries to add things, because there are pluses... Now how can I convert that mess to string right when it comes in? Or is there a better way to do it? I'm sure there is but this ugliness is strictly for me, so I don't care too much about fast performance :)
You need some quotes around the song_uri string. I can't paste code on mobile... but the php should output like this
Onclick="PlaySong ('this+song')"
At the moment you've got it doing
Onclick="PlaySong(this+song)" and the argument isn't being treated as a string
I have an HTML form generated by php which has a dropdown of voucherproviders.
I want to select the provider and have this populate the form for editing.
Here is my JQuery code:
$(document).ready(function() {
$(document).on('change','#id_voucherprovider',function(){
var voucher_providers = <?php echo json_encode($voucher_providers); ?>;
//The value I have got from the drop down is....
var value = $('#id_voucherprovider option:selected').val();
var vendortext = $('#id_voucherprovider option:selected').text();
//so the voucher provider is
alert(vendortext);
$('.ftext input').val(vendortext);
$("textarea#id_vendornotes").val(voucher_providers[value]);
});
});
voucherproviders is not being passed into the JQuery despite the echo json_encode($voucher_providers); code working when inline with the php code.
It seems to return a null array. Can anyone see what is wrong?
Many Thanks
Dave
var voucher_providers = <?php echo json_encode($voucher_providers); ?>;
json_encode returns a string, unless it is parsed it will not be usable. Use JQuery .parseJSON and you should have better results. :)
var jsonString = <?php echo json_encode($voucher_providers); ?>;
var voucher_providers = $.parseJSON(jsonString);
try this (note the quotes):
var voucher_providers = JSON.parse('<?php echo json_encode($voucher_providers); ?>');
parse your json ex: obj = JSON.parse(data);
Hi please check the json format at below site:-
http://jsonlint.com/
and check if this is fine.
i have a html table and it has expandable row
but when i added json encode code to the javascript code. the html table row expand without the user interaction. when the page is loaded. it just expand. and i need to put the code in a sigle javascript because i need to use the data from other javascript function. hope my question is clear. sorry for my bad english
<script type='text/javascript'>//<![CDATA[
var tracknumberreceiveglobal;
$(document).ready(function(){
$("#report tr:odd").addClass("odd");
$("#report tr:not(.odd)").hide();
$("#report tr:first-child").show();
$("#report tr.odd").click(function(){
tracknumberreceiveglobal = $(this).closest("tr").find("td:eq(0)").text();
$(this).next("tr").toggle();
$(this).find('i').toggleClass('glyphicon-plus-sign').toggleClass('glyphicon-minus-sign');
});
});
function functiontwo() {
var tnum = <?php echo json_encode($_tempp1); ?>;
var tsign = <?php echo json_encode($_temppp1); ?>;
var signatoryidglobal = <?php echo json_encode($_SESSION['signatoryid']); ?>
}
</script>
this is the page looks like without json encode
this is the page looks like with json encode
The PHP function json_encode() returns a string (a JSON representation) and not an actual JavaScript object. You have to parse it in order to make use of the data passed through JSON.
var tnum = JSON.parse('<?php echo json_encode($_tempp1); ?>');
var tsign = JSON.parse('<?php echo json_encode($_temppp1); ?>');
var signatoryidglobal = JSON.parse('<?php echo json_encode($_SESSION['signatoryid']); ?>');
You can now access properties using dot notation or brackets.
I am using the following to encode the html source of a ckeditor in a web application.
var updateString = app.getValue('wysiwygHomePage');
var encodedString = encodeURIComponent(updateString);
alert(encodedString);
app.httpRequest("www.xxxx.com/techy/savealldata.php", "GET", function(data, error, httpResponse){
alert(data);
},
{
"updateType":"homePage","updateString":encodedString}, "String", {}, {});
}
Then at the PHP end I am using :
<?php
$updateType = $_GET["updateType"];
$updateString = $_GET["updateString"];
$updateString2 = urldecode($updateString);
echo 'success here '.$updateType .' '.$updateString2 ;
?>
I am adding some coloured tex and the html source for this is:
<p>
<span style="color: rgb(255, 140, 0);">123</span><br />
</p>
<p>
This works okay until I cut and paste more than 32 times.
I then just get error returned from the PHP call.
I presume there are to many chars arriving at the PHP end ???
Any ideas why this is happening ?
Mr WARBY.
UPDATED PHP Code.
<?php
include 'dbdata.php';
$updateType = $_POST["updateType"];
$updateString = $_POST["updateString"];
$updateString2 = urldecode($updateString);
//echo 'success here '.$updateType .' '.$updateString2 ;
if($updateType === 'homePage')
{
$query5 = "UPDATE pageText SET HTML= "."'".$updateString2."'"." WHERE ID = 12";
//echo $query5;
echo 'Home Page Updated 2';
mysql_query($query5);
}
if($updateType === 'instructionPage')
{
$query5 = "UPDATE pageText SET HTML= "."'".$updateString2."'"." WHERE ID = 13";
echo 'Instruction Page Updated 2';
mysql_query($query5);
}
if($updateType === 'FAQPage')
{
$query5 = "UPDATE pageText SET HTML= "."'".$updateString2."'"." WHERE ID = 14";
echo 'FAQ Page Updated';
mysql_query($query5);
}
?>
There are a lot of variables in play here. You need to change your debugging strategy. Instead of testing end to end each time try isolating each component.
In Javascript, call "app.getValue('wysiwygHomePage')", encode the string, decode the string, and put it right back in the editor. Do that in a loop until you can determine if the client-side is mangling anything.
If not, try encoding a complicated string in Javascript, sending it to a PHP script that decodes/re-encodes and echos it back. Do that in a loop several times.
If you still haven't found the problem try making a PHP script that takes a complicated string, INSERTS it, SELECTs it, UPDATEs it in a loop to see if you database encoding or escaping is affecting it.
If at any point you find the string changing when it shouldn't you've probably found your problem.