I want to set UIWebView with htmlstring and I have problem with an image:
...
<img align="absBottom" alt="" height="350" src="http://www.xyz.com//files/userfiles/images/abc.jpg" vspace="10" width="625" />
...
I want to replace height and width to 200 and 320.
I'm using
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"width=\"" withString:#"width=\"320];
then I've got result :
<img align="absBottom" alt="" height="350" src="http://www.xyz.com//files/userfiles/images/abc.jpg" vspace="10" width="320625" />
How to delete the number after the width from htmlString or what is the right way to replace the width to 320px?
Try this using NSScanner:
NSScanner *theScanner;
NSString *subStrng =nil;
theScanner = [NSScanner scannerWithString:htmlString];
[theScanner scanUpToString:#"width" intoString:NULL] ;
[theScanner scanUpToString:#" " intoString:&subStrng] ;
htmlString = [htmlString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#"%#", subStrng] withString::#"width=\"320\""];
NSLog("%#",subStrng);
Try this:
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"width=\"625" withString:#"width=\"320];
Related
i have a html text like that i get from database, but it have a hover that show the image name, so i want to remove the title
from this
<img title="AAAAAAA" src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg" alt="" width="212" height="212">
to this
<img title="" src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg" alt="" width="212" height="212">
i have tried
var text = '<img title="AAAAAAA" src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg" alt="" width="212" height="212">'
text.replace(/(<img .*title=")(.+)(")/, '$1$3')
but it also remove my src
"<img title="">"
any help guys ?
You could use JS function removeAttribute():
document.getElementById("someId").removeAttribute("title");
console.log(document.getElementById("someId"));
<img title="AAAAAAA" src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg" alt="" width="212" height="212" id="someId">
EDIT: How to remove with regex
var text = '<img title="AAAAAAA" src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg" alt="" width="212" height="212">';
console.log(text.replace(/ title=".[^"]*"/, ''))
How to remove only titleĀ“s value:
var text = '<img title="AAAAAAA" src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg" alt="" width="212" height="212">';
console.log(text.replace(/title=".[^"]*"/, 'title=""'))
If you are only getting the element as a string and not as a DOM element, then you can try using \"[\s\S]+(?= ) to select the portion of string that goes from the first " to the first space (returning you "AAAAAAA").
You can then replace the text as per your code in the question.
It can be very hard, if not impossible, to reliably parse HTML using the likes of regex, indexOf, etc. The whole issue can be totally avoided by using the built-in DOMParser object, as follows:
const html = '<img title="AAAAAAA" src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg" alt="" width="212" height="212">';
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
const img = doc.body.children[0];
img.title = "";
console.log(img.outerHTML)
You could also use img.removeAttribute("title"); if you want to get totally rid of the title attribute.
Simpler and cleaner way.
var xmlString = '<img title="AAAAAAA" src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg" alt="" width="212" height="212">'
var x = new DOMParser().parseFromString(xmlString, "text/xml").documentElement
x.removeAttribute("title")
console.log(x)
I am trying to write a regex that looks for an img tag where there is no alt attribute.
Here is the regex.
<img\s+src.+^(?!alt).+$
Here are some samples
<img src="smiley.gif" alt="Smiley face" height="42" width="42">
<img src="smiley2.gif" height="42" width="42">
<img src="smiley3.gif" alt="Smiley face Three" height="42" width="42">
Here is a link to regex101
https://regex101.com/r/Z5vkQb/3/
Don't.
You haven't specified which language you're using, but chances are good you have a DOM parser available.
For example, in JavaScript, you can just do this:
var imgs_with_no_alt = document.querySelectorAll("img:not([alt])");
In PHP you would need something like this:
$dom = new DOMDocument();
$dom->loadHTML("your HTML here");
$images = $dom->getElementsByTagName("img");
foreach($images as $image) {
if( $image->getAttribute("alt")) continue;
// do something to $image here, which doesn't have an [alt] attribute
}
If within the same line, you can use this,
<img(?!.*\s+alt\s*=).+$
Demo
You can use this expression:
<img[^>]*alt=[^>]*>
<a idref="_5AD480AC_7D6C_4AA6_B003_F1C5F38F4D15"
sectionid="n1e14b600d6dec639" internal="true" type="text"
slug="histologie/quergestreifte+muskulatur+histologie" link="" ui-
sref="main.learn.content({slug:
'histologie/quergestreifte+muskulatur+histologie', '#':
'_5AD480AC_7D6C_4AA6_B003_F1C5F38F4D15'})" webtrends-track-on-click="
{'WT.i_module': 'Textlink - Intern'}" >
ultrastrukturelle Aufbau der Myofilamente
</a>
I have above content as response. I just want to change that string to below.
How to get:
href="/lernmodule/histologie/quergestreifte+muskulatur+histologie#_5AD480AC_7D6C_4AA6_B003_F1C5F38F4D15"
from the above string?
If the format of your response is specific like you shown in your question then below code will work for you:
NSString *match = #"internal";
NSString *postTel;
NSString *preTel;
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner scanUpToString:match intoString:&preTel];
[scanner scanString:match intoString:nil];
postTel = [str substringToIndex:scanner.scanLocation];
NSString*requiredString=[[postTel substringFromIndex:3] stringByReplacingOccurrencesOfString:#"internal" withString:#""];
strContent=[strContent stringByReplacingOccurrencesOfString:#"', '#': '" withString:#"'#"];
strContent=[strContent stringByReplacingOccurrencesOfString:#"'})\" web" withString:#"\" web"];
strContent=[strContent stringByReplacingOccurrencesOfString:#"({slug: '" withString:#" \" href=\"/lernmodule/"];
strContent=[strContent stringByReplacingOccurrencesOfString:#"\"\"#" withString:#"#"];
I'm using WKWebView loading html string, some end of html string have a few of ugly image links, i want to hide them.
The css use to hide image, but not works.
.article img[src* = "/smilies/"],
.article img[src* = ".feedburner.com/~ff/"],
.article img[src* = ".feedburner.com/~r/"],
.article img[src* = ".feedblitz.com/"]
{
display: none;
}
The sample html string with feedburner src i want to hide :
<div>
<img src="http://feeds.feedburner.com/~ff/Venturebeat?d=yIl2AUoC8zA" border="0"> <img src="http://feeds.feedburner.com/~ff/Venturebeat?d=qj6IDK7rITs" border="0"> <img src="http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU" border="0"> <img src="http://feeds.feedburner.com/~ff/Venturebeat?d=I9og5sOYxJI" border="0"> <img src="http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk" border="0">
</div>
A quick and dirty way to achieve this is by using regular expressions. Mind you that this is really not ideal for long HTML files as it is not as efficient as a real HTML parser.
// The HTML you posted
NSString *HTML = #"<div>\n\t<img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=yIl2AUoC8zA\" border=\"0\"> <img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=qj6IDK7rITs\" border=\"0\"> <img src=\"http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:V_sGLiPBpWU\" border=\"0\"> <img src=\"http://feeds.feedburner.com/~ff/Venturebeat?d=I9og5sOYxJI\" border=\"0\"> <img src=\"http://feeds.feedburner.com/~ff/Venturebeat?i=H9eoOCii8XI:sanX3-jfWnw:D7DqB2pKExk\" border=\"0\">\n</div>";
// A string containing source of the images that you want to delete
NSString *source = #"http://feeds.feedburner.com/~ff/";
// Builds a pattern that matches the tags of the images you want to delete
NSString *pattern = [NSString stringWithFormat:#"<img src=\"%#.+?>", source];
// The actual delete operation
NSString *cleanHTML = [HTML stringByReplacingOccurrencesOfString:pattern
withString:#""
options:NSRegularExpressionSearch
range:NSMakeRange(0, HTML.length)];
// Do what you want with the cleaned HTML (display it, ...)
NSLog(#"%#", cleanHTML);
I am trying to retrieve the image from this html data:
<div class="image">
<a href="http://www.website.com/en/105/News/10217/">
<img src="/images/cache/105x110/crop/images%7Ccms-image-000005554.gif"
width="105" height="110" alt="kollsge (photo: author)" />
</a>
</div>
This is my code:
HTMLNode *bodyNode = [parser body];
NSArray *imageNodes = [bodyNode findChildTags:#"div"];
for (HTMLNode *imageNode in imageNodes) {
if ([[imageNode getAttributeNamed:#"class"] isEqualToString:#"image"]) {
NSLog(#"%#", [imageNode getAttributeNamed:#"img src"]);
}
}
Help would be much appreciated.
I solved it by this code:
for (HTMLNode *imageNode in imageNodes) {
if ([[imageNode getAttributeNamed:#"class"] isEqualToString:#"image"]) {
HTMLNode *aNode = [imageNode firstChild];
HTMLNode *imgNode = [aNode nextSibling];
HTMLNode *imNode = [imgNode firstChild];
NSLog(#"%#", [imNode getAttributeNamed:#"src"]);
}
}
You are not going through the tree correctly. You are attempting to find an attribute named img src on your div. That would look like this:
<div class="image" img src="whatever">
For one thing, that's not valid HTML, but the more important issue is that you want to be looking at the children. The thing you are looking for is nested inside the div, not an attribute. Since your div only has one child, a quick look at the project you provided in the comments leads me to believe that the following will work:
HTMLNode *bodyNode = [parser body];
NSArray *imageNodes = [bodyNode findChildTags:#"div"];
for (HTMLNode *imageNode in imageNodes) {
if ([[imageNode getAttributeNamed:#"class"] isEqualToString:#"image"]) {
HTMLNode *aNode = [imageNode firstChild];
HTMLNode *imgNode = [aNode nextSibling];
NSLog(#"%#", [imgNode getAttributeNamed:#"src"]);
}
}