I have the following code that will check to see if the div text has change and if so then update the text in the mysql table. but when i try and add spaces in the text and it dont allow it to save every thing i have write. Can somebody help me out please
Thank you.
<script>function save() {
var div_sN6VmIGq = $("#64").text();
var html_sN6VmIGq = $("#64").html();
var top_sN6VmIGq = $("input#sN6VmIGq_top").val();
var left_sN6VmIGq = $("input#sN6VmIGq_left").val();
if(div_sN6VmIGq == "Text Here"){
}else{
$('#saveupdate').load('modulus/empty/actions.php?act=save_text&pid=1&div_id=64&div_txt='+html_sN6VmIGq+'&randval='+ Math.random());
}
}
</script>
You need to URLEncode your text:
JavaScript:
var html_sN6VmIGq = escape($("#64").html());
//now use your function for saving the item
PHP:
$decodedVar = urldecode($yourEncodedVarHere);
You probably need to call encodeURI on your somewhat bizarrely named variables:
$('#saveupdate').load('modulus/empty/actions.php?act=save_text&pid=1&div_id=64&div_txt='+encodeURI(html_sN6VmIGq)+'&randval='+ Math.random());
Also, note that the id 64 is invalid, per this comment from W3Schools:
[ids must] begin with a letter A-Z or a-z followed by: letters (A-Za-z),
digits (0-9), hyphens ("-"),
underscores ("_"), colons (":"), and
periods (".").
Related
Please take a look at the following situation below.
[reply="292"] Text Here [/reply]
What I am trying to get is the number between the quotations in reply="NUMBERS". I want to extract that to one variable and the text between [reply="NUMBER"] this text here [/reply] to another variable.
So for this example:
[reply="292"] Text Here [/reply]
I want to extract the reply number: 292 and the text between the reply tags: Text here.
I have tried this:
\[reply\=\"]([A-Z]\w)\[\/reply]
But this only works until the reply tag, doesn't work after that. How can I go about doing this?
I left generic (. *), but you can specify a type like decimal (\d+).
php:
$s = '[reply="292"] Text Here [/reply]';
$expr = '/\[reply=\"(.*)\"\](.*)\[\/reply\]/';
if(preg_match($expr,$s,$r)){
var_dump($r);
}
javascript:
s = '[reply="292"] Text Here [/reply]'
s.match(/\[reply=\"(.*)\"\](.*)\[\/reply\]/)
//["[reply="292"] Text Here [/reply]", "292", " Text Here "]
Easy!
\[reply\=\"(\d+)\"](.*?)\[\/reply]
Explanation
\d for digit
+ for 1 or more occurrence of the specified character.
[\w\s] for any character in word and whitespace (\s)
Then apply it to PHP like this:
<?php
$str = "[reply=\"292\"] Text Here [/reply]";
preg_match('/\[reply\=\"(\d+)\"]([\w\s]+)\[\/reply]/', $str, $re);
print_r($re[1]); // printing group 1, the reply number
print_r($re[2]); // printing group 2, the text
?>
Important!!
Just get the group value, not all. You only need some of it anyway.
i want to replace space with - in a tag -->href attribut in php smarty;
that a[key] is dynamic
what is the way?
{$obj->a[key]}
This could be answer for you
{$obj-a[key]|replace:' ':'-'}
http://www.smarty.net/docsv2/en/language.modifier.replace.tpl
you should try this
str_replace() is a php function which replace the character between
sentences. there are basically three argument pass in the function.
First argument: search the character, Second argument: Replacing Character,
Third argument: sentences.
<?php
$str='home and car';
echo ''.str_replace(' ','-',$str).'';
?>
output
jquery code
g is a regex code which replace all the space between the string.
<script>
$("a").each(function() {
var text = $(this).text();
text = text.replace(/ /g, "-");
$(this).prop('href',text);
$(this).text(text);
});
</script>
Output
home-and-car
My current code is:
$epattern[17] = "/#(\\w+)/";
$ereplace[17] = "<a href=viewprofile.php?username=$1><font color=royalblue><b>#\\1</b></font></a>";
$postinforawb = preg_replace($epattern,$ereplace,$postinfo);
With the above code the text will highlight blue where the # symbol was used up to where a space has been entered. However I now also want it to include the "+" symbol in posts. So that the following would highlight blue: "#First+Second"
What am I needing to add to the replace?
This will do in your case:
$epattern[17] = "/#([\w\+]+)/";
But i prefer this one as you are only allowing alphabet and +:
$epattern[17] = "/#([a-zA-Z\+]+)/";
$epattern[17] = "/#([\w\+]+)/";
I'm using the code below for highlight one word from file_get_content and go to anchor.
$file='
IAR6=1002
SHF6=1
REF6=0002
TY7=2
DATE7=20130820182357
STAT_N7=1002
SEQ7=0002110000001
STA7=000005
TY8=2
DATE8=20130820182429
STAT_N8=1002
SH8=1
OP8=S123
SEQ8=0002120000081
';
$Seq = 0002110000001;
$text = preg_replace("/\b($Seq)\b/i", '<span class="highlight"><a name="here">\1</a></span>', $file);
for now this highlight : 0002110000001
i would like to highlight all part of the same index number.
ex:
looking for 0002110000001
highlight this part of txt only where number is 7
TY7=2
DATE7=20130820182357
STAT_N7=1002
SEQ7=0002110000001
STA7=000005
Any help will be appreciated.
EDIT:
i try to be more specific.
file contain lot of code parts always start by TYx (x is auto numbering)
i have the SEQ number for my search , in ex 0002110000001
the preg_replace("/\b($Seq)\b/i", '\1 find 0002110000001 and higlight them.
what i need is higlight what is between TY7 and TY8 instead of only 0002110000001.
Hope this is clear enough due to my bad english
thanks
You can make use of stripos() and explode() in PHP
<?php
$file='
IAR6=1002
SHF6=1
REF6=0002
TY7=2
DATE7=20130820182357
STAT_N7=1002
SEQ7=0002110000001
STA7=000005
TY8=2
DATE8=20130820182429
STAT_N8=1002
SH8=1
OP8=S123
SEQ8=0002120000081
';
//$Seq = "0002110000001";
$Seq = "7";
$new_arr=explode(PHP_EOL,$file);
foreach($new_arr as $k=>$v)
{
if(stripos($v,$Seq)!==false)
{
echo "$v\n";
}
}
OUTPUT :
TY7=2
DATE7=20130820182357
STAT_N7=1002
SEQ7=0002110000001
STA7=000005
I need to match and replace some comments.
for example:
$test = "the url is http://www.google.com";// comment "<-- that quote needs to be matched
I want to match the comments outside of the quotes, and replace any "'s in the comments with "'s.
I have tried a number of patterns and different ways of running them but with no luck.
The regex will be run with javascript to match php "//" comments
UPDATE:
I took the regex from borkweb below and modified it. used a function from http://ejohn.org/blog/search-and-dont-replace/ and came up with this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function t_replace(data){
var q = {}, ret = "";
data.replace(/(?:((["'\/]*(("[^"]*")|('[^']*'))?[\s]*)?[\/\/|#][^"|^']*))/g, function(value){
q[key] = value;
});
for ( var key in q ){
ret = q[key];
}
var text = data.split(ret);
var out = ret + text[1];
out = out.replace(/"/g,""");
out = out.replace(/'/g,"'");
return text[0] + out;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(t_replace("$test = \"the url is http://www.google.com\";// c'o\"mment \"\"\"<-- that quote needs to be matched")+"<br>");
document.write(t_replace("$test = 'the url is http://www.google.com';# c'o\"mment \"\"\"<-- that quote needs to be matched"));
</script>
</body>
</html>
it handles all the line comments outside of single or double quotes. Is there anyway I could optimize this function?
UPDATE 2:
it does not handle this string
document.write(t_replace("$test //= \"the url is http://www.google.com\"; //c'o\"mment \"\"\"<-- that quote needs to be matched")+"<br>");
You can have a regexp to match all strings and comments at the same time. If it's a string, you can replace it with itself, unchanged, and then handle a special case for comments.
I came up with this regex:
"(\\[\s\S]|[^"])*"|'(\\[\s\S]|[^'])*'|(\/\/.*|\/\*[\s\S]*?\*\/)
There are 3 parts:
"(\\[\s\S]|[^"])*" for matching double quoted strings.
'(\\[\s\S]|[^'])*' for matching single quoted strings.
(\/\/.*|\/\*[\s\S]*?\*\/) for matching both single line and multiline comments.
The replace function check if the matched string is a comment. If it's not, don't replace. If it is, replace " and '.
function t_replace(data){
var re = /"(\\[\s\S]|[^"])*"|'(\\[\s\S]|[^'])*'|(\/\/.*|\/\*[\s\S]*?\*\/)/g;
return data.replace(re, function(all, strDouble, strSingle, comment) {
if (comment) {
return all.replace(/"/g, '"').replace(/'/g, ''');
}
return all;
});
}
Test run:
Input: $test = "the url is http://www.google.com";// c'o"mment """<-- that quote needs to be matched
Output: $test = "the url is http://www.google.com";// c'o"mment """<-- that quote needs to be matched
Input: $test = 'the url is http://www.google.com';# c'o"mment """<-- that quote needs to be matched
Output: $test = 'the url is http://www.google.com';# c'o"mment """<-- that quote needs to be matched
Input: $test //= "the url is http://www.google.com"; //c'o"mment """<-- that quote needs to be matched
Output: $test //= "the url is http://www.google.com"; //c'o"mment """<-- that quote needs to be matched
Don't forget that PHP comments can also take the form of /* this is a comment */ which can be span across multiple lines.
This site may be of interest to you:
http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript
Javascript does not have native lookbehind support in it's regular expression engine. What you may be able to do is start at the end of a line and look backward to capture any characters that follow a semi colon + optional whitespace + // So something like:
;\w*\/\/(.+)$
This may not capture everything.
You also may want to look for a Javascript (or other languages) PHP syntax checker. I think Komodo Edit's PHP syntax checker may be written in Javascript. If so, it may give you insight on how to strip everything out but comments as the syntax checkers need to ensure the PHP code is valid, comments and all. The same can be said about syntax color changers. Here are two other links:
http://ecoder.quintalinda.com/
http://www.webdesignbooth.com/9-useful-javascript-syntax-highlighting-scripts/
I have to admit, this regex took me a while to generate...but I'm pretty sure this will do what you are looking for:
<script>
var str = "$test = \"the url is http://www.google.com\";// comment \"\"\"<-- that quote needs to be matched";
var reg = /^(?:(([^"'\/]*(("[^"]*")|('[^']*'))?[\s]*)?\/\/[^"]*))"/g;
while( str !== (str = str.replace( reg, "$1"") ) );
console.log( str );
</script>
Here's what's going on in the regex:
^ # start with the beginning of the line
(?: # don't capture the following
(
([^"'\/]* # start the line with any character as long as it isn't a string or a comment
(
("[^"]*") # grab a double quoted string
| # OR
('[^']*') # grab a single quoted string
)? # but...we don't HAVE to match a string
[\s]* # allow for any amount of whitespace
)? # but...we don't HAVE to have any characters before the comment begins
\/\/ # match the start of a comment
[^"]* # match any number of characters that isn't a double quote
) # end un-caught grouping
) # end the non-capturing declaration
" # match your commented double quote
The while loop in javascript is just find/replacing until it can't find any additional matches in a given line.
In complement of #Thai answer which I found very good, I would like to add a bit more:
In this example using original regex only the last character of quotes will be matched: https://regex101.com/r/CoxFvJ/2
So I modified a bit to allow capture of full quotes content and give a more talkative and generic example of content: https://regex101.com/r/CoxFvJ/3
So final regex:
/"((?:\\"|[^"])*)"|'((?:\\'|[^'])*)'|(\/\/.*|\/\*[\s\S]*?\*\/)/g
Big thanks to Thai for unlocking me.