如何从 React Native 项目的 WordPress API 端点获取的数据中删除 HTML 个字符?
How do I remove HTML characters from the data fetched from a WordPress API endpoint in a React Native project?
我正在做一个 React Native 项目,该项目要求我从 WordPress 网站获取 posts,
这是我的代码
render() {
if (this.state.isLoading){
return(
<View style={{flex: 1, justifyContent: 'center',}}>
<ActivityIndicator />
</View>
)
}
else{
let posts = this.state.data.map((post, index) => {
function strip_html_tags(str)
{
if ((str===null) || (str===''))
return false;
else
str = str.toString();
return str.replace(/<[^>]*>/g, '');
}
return (
<ListItem thumbnail key={index}>
<Left>
<Thumbnail square source={{ uri: post.jetpack_featured_media_url != null ? post.jetpack_featured_media_url : 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACWBAMAAADOL2zRAAAAG1BMVEXMzMyWlpajo6PFxcW3t7ecnJyqqqq+vr6xsbGXmO98AAAACXBIWXMAAA7EAAAOxAGVKw4bAAABPUlEQVRoge3Tv0/CQBjG8YcWaMcebymOENLI2MZoHMHEvVUKjq1K4lhM2Kvxx7/tUUiamDhc6GSez8INzbf3HleAiIiIiIiIiIiIiNozAGzvuJYTW2reXmso7bX8YN96HUR1a7RZ6+VVOgU+p4LuZGrSkqK0PWfwfl+3ht/hcpdvPkJ0g0fBYpYZtS7HttfPMatbAbZzJ1kjjnqVK1ihNzdpdX3b65S4qVsjXbG9EtuoEzliC/RbDFoIL7wY2NZrQayPzw1VpH/FUUqNjVrx0+9W8Rzrlt7yMMvMWq7fzHhoCTp6Rr0vw0uiH8+as69bov/AyNqf/Rms3Ky1aO7EYV93X2nlBIXg7WVSmrWs5q4eWrvVdYLbpR4/PTeZ8S9O82mdzMr7SVstV6mqrRaKh9ZSRERERERERET0n/wAZwMqI9kyPcoAAAAASUVORK5CYII=' }} />
</Left>
<Body>
<Text>{strip_html_tags(post.title.rendered)}</Text>
<Text note numberOfLines={2}>{strip_html_tags(post.excerpt.rendered)}</Text>
</Body>
<Right>
<Button transparent>
<Text>View</Text>
</Button>
</Right>
</ListItem>
);
});
post 渲染了一些 HTML 特殊字符,我用这段代码删除了一些
function strip_html_tags(str)
{
if ((str===null) || (str===''))
return false;
else
str = str.toString();
return str.replace(/<[^>]*>/g, '');
}
但有些人不会出去。我仍然明白:
Screenshot
解决方案是使用 react-native-render-html 或 react-native-webview 库。
Wordpress rest api 响应包含一些 html 标签,因此需要在 App.
上删除它
我正在做一个 React Native 项目,该项目要求我从 WordPress 网站获取 posts,
这是我的代码
render() {
if (this.state.isLoading){
return(
<View style={{flex: 1, justifyContent: 'center',}}>
<ActivityIndicator />
</View>
)
}
else{
let posts = this.state.data.map((post, index) => {
function strip_html_tags(str)
{
if ((str===null) || (str===''))
return false;
else
str = str.toString();
return str.replace(/<[^>]*>/g, '');
}
return (
<ListItem thumbnail key={index}>
<Left>
<Thumbnail square source={{ uri: post.jetpack_featured_media_url != null ? post.jetpack_featured_media_url : 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACWBAMAAADOL2zRAAAAG1BMVEXMzMyWlpajo6PFxcW3t7ecnJyqqqq+vr6xsbGXmO98AAAACXBIWXMAAA7EAAAOxAGVKw4bAAABPUlEQVRoge3Tv0/CQBjG8YcWaMcebymOENLI2MZoHMHEvVUKjq1K4lhM2Kvxx7/tUUiamDhc6GSez8INzbf3HleAiIiIiIiIiIiIiNozAGzvuJYTW2reXmso7bX8YN96HUR1a7RZ6+VVOgU+p4LuZGrSkqK0PWfwfl+3ht/hcpdvPkJ0g0fBYpYZtS7HttfPMatbAbZzJ1kjjnqVK1ihNzdpdX3b65S4qVsjXbG9EtuoEzliC/RbDFoIL7wY2NZrQayPzw1VpH/FUUqNjVrx0+9W8Rzrlt7yMMvMWq7fzHhoCTp6Rr0vw0uiH8+as69bov/AyNqf/Rms3Ky1aO7EYV93X2nlBIXg7WVSmrWs5q4eWrvVdYLbpR4/PTeZ8S9O82mdzMr7SVstV6mqrRaKh9ZSRERERERERET0n/wAZwMqI9kyPcoAAAAASUVORK5CYII=' }} />
</Left>
<Body>
<Text>{strip_html_tags(post.title.rendered)}</Text>
<Text note numberOfLines={2}>{strip_html_tags(post.excerpt.rendered)}</Text>
</Body>
<Right>
<Button transparent>
<Text>View</Text>
</Button>
</Right>
</ListItem>
);
});
post 渲染了一些 HTML 特殊字符,我用这段代码删除了一些
function strip_html_tags(str)
{
if ((str===null) || (str===''))
return false;
else
str = str.toString();
return str.replace(/<[^>]*>/g, '');
}
但有些人不会出去。我仍然明白:
Screenshot
解决方案是使用 react-native-render-html 或 react-native-webview 库。 Wordpress rest api 响应包含一些 html 标签,因此需要在 App.
上删除它