I am reading data over a socket and parsing it with LibXML.
The problem that I am currently having is that sometimes there is a web link in the data which breaks the parser.
http://example.com/?key=value&key2=value
Is there any way to convert that to html characters?
Something like the above to
http://example.com/?key=value&key2=value
Example of socket data:
<node link="http://example.com/?key=value&key2=value" />
EDIT:
Found a solution that works for my problem here
You are going to have to do a pre-filter here. Contrary to other indications, search and replace just won't cut it. Consider your search side is &, which matches too much.
Construct the following finite state machine:
NORMAL:
if next matches "<" then TAG
TAG:
if next matches "![CDATA[" then CDATA
TAGSCAN
TAGSCAN:
if next matches whitespace then TAGSCAN2
if next matches > or next matches /> then NORMAL
TAGSCAN2:
if next matches whitespace then TAGSCAN2
if next matches SRC= or next matches HREF= then URL
TAGSCAN
URL:
we found an attribute with a URL in it. Do your search and replace
on the contents of the URL attribute value, advance past the URL and
go back to TAGSCAN
CDATA:
if next doesn't match ]]> then CDATA
NORMAL
I have found a nice solution using the code from
Find and Replace that uses a Find and Replace method suggested by bolov.
retval = str_replace(message, size, "&", "&");
if (!retval) {
printf("Not enough room to replace & with `&'\n");
}
Related
I am trying to save category name after convert in slug
So in entity I have used setter for convert my text to slug text
protected function _setName($name)
{
return Text::slug($name);
}
After send post request in input name "আমি তোমায় ভালোবাসি"
Has got in database
ami-tomaya-bhalobasi
After make transliteratorId false
return Text::slug($name,[
'transliteratorId' => false
]);
I got output : আম-ত-ম-য-ভ-ল-ব-স
My expected result is
আমি-তোমায়-ভালোবাসি
How can I get my desire result ?
The whole point of slugs is to obtain a "safe" pure US-ASCII string. If all you seemingly want is to remove white spaces you can use a simple regular expression:
preg_replace('/\s/u', '-', 'আমি তোমায় ভালোবাসি')
However, I recommend you double-check why you think this is necessary in the first place. A properly encoded URL would display spaces as %20 anyway, which is "ugly" in a Latin script text but will get unnoticed in other scripts:
var_dump(rawurlencode('আমি তোমায় ভালোবাসি'));
string(159) "%E0%A6%86%E0%A6%AE%E0%A6%BF%20%E0%A6%A4%E0%A7%8B%E0%A6%AE%E0%A6%BE%E0%A6%AF%E0%A6%BC%20%E0%A6%AD%E0%A6%BE%E0%A6%B2%E0%A7%8B%E0%A6%AC%E0%A6%BE%E0%A6%B8%E0%A6%BF"
૮₍ • ᴥ • ₎ა・Raiden ▬▭⋱𓂅
ᘏ⑅ᘏ╭╯Welcome╰╮𓂃ᘏᗢ
・・・・・・・・・・・・・・・・・・・・・
https://discord.gg/rsCC8y7WC4
・・・・・・・・・・・・・・・・・・・・・
Join!
・・・・・・・・・・・・・・・・・・・・・
How can I pull the "discord.gg/rsCC8y7WC4" link from a text like this
console.log(invitelink) ==> discord.gg/rsCC8y7WC4
Use String.match() for this. String.match accepts a regex argument which looks like this:
let str = 'hey there this is just a random string';
let res = str.match(/random/);
//res is now ['random']
Now for your problem, you are probably looking for this:
if(msg.content.match(/discord\.gg\/.+/) || msg.content.match(/discordapp\.com\/invite\/.+/)) return msg.channel.send('Hey! You put an invite in your message!');
Now that regex may look a bit messy/complicated but the \s are to escape the character and make sure regex knows that it’s not the special character it uses, and is actually just a character part of the search. To clarify why the above example should work, here’s a little explanation:
match() returns either an array (if it gets a match) or null (if it gets no match). You are searching for the strings 'discord.gg/' followed by any characters and also checking for the string 'discordapp.com/invite/' also followed by any characters.
If this doesn’t work, please tell me.
Got quite a head-scratcher....
I'm using the VBScript function REPLACE to replace spaces in a decrypted field from a MSSQL DB with "/".
But the REPLACE function isn't "seeing" the spaces.
For example, if I run any one of the following, where the decrypted value of the field "ITF_U_ClientName_Denc" is "Johnny Carson":
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/")
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/")
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"),"Chr(160)","/")
REPLACE(CSTR(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"))," ","/")
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/",1,-1,1)
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/",1,-1,0)
The returned value is "Johnny Carson" (space not replaced with /)
The issue seems to be exclusively with spaces, because when I run this:
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"),"a","/")
I get "Johnny C/rson".
Also, the issue seems to be exclusively with spaces in the decrypted value, because when I run this:
REPLACE("Johnny Carson"," ","/")
Of course, the returned value is "Johnny/Carson".
I have checked what is being written to the source of the page and it is simply "Johnny Carson" with no encoding or special characters.
I have also tried the SPLIT function to see if it would "see" the space, but it doesn't.
Finally, thanks to a helpful comment, I tried VBS REGEX searching for \s.
Set regExp = New RegExp
regExp.IgnoreCase = True
regExp.Global = True
regExp.Pattern = "\s" 'Add here every character you don't consider as special character
strProcessed = regExp.Replace(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"), "?")
Unfortunately, strProcessed retruns "Johnny Carson" (ie. spaces not detected/removed).
If I replace regExp.Pattern = "a", strProcessed returns "Johnny C?rson".
Many thanks for your help!!
As we found, the right character code is 160, and that did the trick:
replace(..., ChrW(160), "...")
This seems to be data specific and, additionally, as an alternative you can try to get same encoding of the source script (i.e. save with Save As with Encoding), or convert received database value into a different target encoding.
I have $location.search('event', data); but problem is that i get in url something like this :
?event=arsenal%20fc%20
and i want to remove this spaces to get : arsenalfc
You can split the data on the spaces and rejoin it with no spaces.
data = data.split(' ').join('');
Update
Based on the conversation in the comments you might be better of replacing with a hyphen. This will give you the nice looking urls you desire whilst giving you the opportunity to re-replace them with spaces at the other end.
Remember to trim your search string first so that you don't get a trailing hyphen:
data.trim().split(' ').join('-')
Just replace the spaces with nothing:
$location.search('event', data.replace(/ /g, ''))
Using ui-router, is there a way to capture a path that contains forward slashes and query parameters?
Let's say there's the catch-all rule in the state configuration:
var exState = {
url: '/example/*path',
...
};
$stateProvider.state(exState);
and then point my browser to
/example/test1?var1=a&var2=b/test2?var3=c&var4=d/
I now see that the forward slashes get encoded:
test1?var1=a&var2=b%2Ftest2%3Fvar3&var4=d%2F
and $stateParams.path is test1 - not what I wanted. Is it possible to get the actual 'raw' path for further processing while avoiding the automatic query parameter capturing which ui-router is doing here?
You can accomplish this but you'll need to use Regex with your URL state, this is from the UI-Router Guide (https://github.com/angular-ui/ui-router/wiki/URL-Routing):
Regex Parameters
A bonus to using curly brackets is the ability to set a Regular Expression rule for the parameter:
// will only match a contactId of one to eight number characters
url: "/contacts/{contactId:[0-9]{1,8}}"
Examples:
'/user/{id:[^/]*}' - Same as '/user/{id}' from the previous example.
'/user/{id:[0-9a-fA-F]{1,8}}' - Similar to the previous example, but only matches if the id parameter consists of 1 to 8 hex digits.
'/files/{path:.*}' - Matches any URL starting with '/files/' and captures the rest of the path into the parameter 'path'.
'/files/*path' - Ditto. Special syntax for catch all.`
This one should catch your url and the parameter string: '/files/{path:.*}' - Matches any URL starting with '/files/' and captures the rest of the path into the parameter 'path'.
EDIT: After capturing the the rest of the URL in the path parameter you'll need to URL decode in order to regain the / & ? and anything else encoded. You can use decodeURIComponent()