how to replace space with - in href with php or jquery - php

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

Related

How to remove plain text from a string after using strip_tags()

So i have a string and I used the strip_tags() function to remove all tags except IMG but I still have plain text next to my IMG element. Here a visual example
$myvariable = "This text needs to be removed<a href='blah_blah_blah'>Blah</a><img src='blah.jpg'>"
So using PHP strip_tags() I was able to remove all tags except the <img> tag (which is what I want). But the thing is now it didn't remove the text.
How do I remove the left over text? Text will always either before tag or after tag as well
[ADDED MORE DETAILS]
$description = 'crazy stuff<img src="https://scontent.cdninstagram.com/t51.2885-15/e15/14287934_1389514537744146_673363238_n.jpg?ig_cache_key=MTMzNzM3MzgwNjAyNDY5NDAzMA%3D%3D.2">';
that's what the variable is actually holding.
Thanks in Advance
Instead of replacing something you can very well extract the values you want:
(<(\w+).+</\2>)
To be used with preg_match(), see a demo on regex101.com.
IN PHP:
<?php
$regex = '~(<(\w+).+</\2>)~';
$string = 'crazy stuff<img src="https://scontent.cdninstagram.com/t51.2885-15/e15/14287934_1389514537744146_673363238_n.jpg?ig_cache_key=MTMzNzM3MzgwNjAyNDY5NDAzMA%3D%3D.2">here as well';
if (preg_match($regex, $string, $match)) {
echo $match[1];
}
?>
Please show your whole piece of code with the use of strip_tags.
You can try: preg_replace('~.*(<img[^>]+>)~', '$1', $myvariable);

Everything between "[reply]" and extract the reply number

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.

Php output breaks the Javascript

i have php variables that is like this
var externalData = '<?php echo $matches[0]; ?>';
I when i load the source code of the page comes like this
var externalData = 'data,data,data,data,data
';
this breaks the javascript code, and browser cant run it.
I want to be like this:
var externalData = 'data,data,data,data,data';
the php output is a full line of a file, so may contains the end of the line.
I test it by hand and working, how i can fix this?
You can use trim (or rtrim) to remove the line break at the end of the string:
var externalData = "<?php echo trim($matches[0]); ?>";
Alternatively you could pass the whole string to json_encode:
var externalData = <?php echo json_encode($matches[0]); ?>;
This would not remove the line break, but it would encode it and the resulting value will be a valid JS string literal (i.e. all other characters that could break the code, such as ' or ", will escaped as well).
Maybe you should strip all HTML
var externalData = "<?php echo strip_tags($matches[0];) ?>");
You can also use substr() to get rid of the last char of string.
Like this:
var externalData = "<?php echo substr($matches[0], 0, strlen($matches[0]) - 1); ?>";

How do I make my preg_replace only look for the words while they are NOT within an <acronym> tag?

In PHP I have a String $string and an array $acronyms (in the form "UK" => "United Kingdom").
Now I want to replace all acronyms within $string by some HTML Tags. For example Hello UK should turn into Hello <acronym title="United Kingdom">UK</acronym></pre>
I do it this way:
foreach($acronyms as $acronym => $tooltip){
$string = preg_replace('/'.$acronym.'/i', ''.$acronym.'', $string);
}
The problem is: Let's say I have a text Hello UK and have an array to replace "UK" with "United Kingdom" and "Kingdom" with "RandomWord". Then the text will replace into Hello <acronym title="United <acronym title="RandomWord">Kingdom</acronym>">UK</acronym> which obviously is chaos.
So the question is: How do I make my preg_replace only look for the words while they are NOT within an <acronym> tag? (neither in title-attribute, nor within the tag itself)
Edit: second attempt according to a response (because I can't put code in reply). Still the same problem, the text within acronym gets replaced a second time...
foreach($acronyms as $acronym => $tooltip){
$acronyms[$acronym] = '<acronym title="'.$tooltip.'">'.$acronym.'</acronym>';
}
$string = str_ireplace(array_keys($acronyms), array_values($acronyms), $string);
You can use strtr(). It doesn't rescan the string after performing a replacement:
foreach ($acronyms as $acronym => $tooltip) {
$acronyms[$acronym] = sprintf('<acronym title="%s">%s</acronym>',
htmlspecialchars($tooltip),
htmlspecialchars($acronym)
);
}
echo strtr($str, $acronyms);
Here's an attempt at the regex version:
foreach($acronyms as $acronym => $tooltip){
$rexp = '/' . $acronym . '(?!((?!<acronym).)*<\/acronym>)/i';
$string = preg_replace($rexp, ''.$acronym.'', $string);
}
Seems to work for me. It does the following:
Match the $acronym variable with a negative look ahead...
where a closing acronym tag can be found
but stop the lookahead when an opening acronym tag is before it.
Ultimately this matches only where it's not within an acronym tag (including all attributes such as the title).
Here's an example of it in action: gSkinner regex example
Don't try to do everything with regexes :
Parse your HTML using a HTML/XML parsing library.
Iterate over your HTML tags, replace what you have to replace.
Ask your "html parsing lib" to convert this back to a "HTML string".

Jquery Not letting Me Save My FULL Text. Only 1 Word

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 (".").

Categories