首页 \ 问答 \ 在PHP中遇到preg_match()问题[关闭](Having an issue with preg_match() in PHP [closed])

在PHP中遇到preg_match()问题[关闭](Having an issue with preg_match() in PHP [closed])

我在PHP上有点生疏,所以请耐心等待。 我正在制作一个简单的电子邮件表单,用于验证几个关键数据。 其中一个字段是提交者的邮政编码。 为了验证这一点,我认为正则表达式很简单,因为我非常有信心可以在一行中完成验证。 看吧:

preg_match("^([0-9]{5}|[0-9]{5}\-[0-9]{4})$", $zip);

这应该与12345和12345-6789相匹配。 但是,这不会发生。 我在这里俯瞰什么?

谢谢!


I am a bit rusty at PHP, so bear with me. I am making a simple email form that validates a few key pieces of data. One of these fields is the submitter's zip code. To validate this, I figured a regex would be simple since I am pretty confident the validation can be done in one line. Behold:

preg_match("^([0-9]{5}|[0-9]{5}\-[0-9]{4})$", $zip);

This should be working to match 12345 and 12345-6789. However, this does not happen. What am I overlooking here?

Thanks!


原文:https://stackoverflow.com/questions/3189559
更新时间:2023-06-25 17:06

最满意答案

这里混合了两种不同的API约定。 使用Shared API时不使用run上下文,它仅在调用Word API时使用 。 一般来说,我会推荐使用Word API,因为它更新,整体上提供了更多的功能。

也就是说,对于此操作,您可以使用Word或Shared API。 我将提供两个例子。

还要注意.toDataURL()返回一个包含 base64图像的字符串。 它不是纯粹的base64表示。 您需要删除Word之前的data:image/png;base64,前缀才能够呈现它。

共享API:

既然你只是插入一个图像,我会使用Office.CoercionType.Image而不是Office.CoercionType.Html

Office.context.document.setSelectedDataAsync(imgHtml, {
        coercionType: "image"
    },
    function (asyncResult) {
        if (asyncResult.status == "failed") {
            writeError('Error: ' + asyncResult.error.message);
        }
    });

Word API:

Word API包含一个insertInlinePictureFromBase64方法。 与setSelectedDataAsync ,它将图像作为Base 64编码的字符串:

Word.run(function (context) {
    context.document.body.insertFileFromBase64(base64String, "end");
    return context.sync();
}).catch(function (myError) {
    // Handle error
})

You're mixing a two different API conventions here. You don't use a run context when using the Shared API, it is only used when calling the Word API. In general, I would recommend using Word API as it's newer and overall provides a more functionality.

That said, for this operation you could use either Word or Shared APIs. I'll provide examples for both.

Also note that .toDataURL() returns a string that includes a base64 image. It isn't a pure base64 representation. You need to remove the data:image/png;base64, prefix before Word will be able to render it.

Shared API:

Since you're simply inserting an image, I would use Office.CoercionType.Image instead of Office.CoercionType.Html:

Office.context.document.setSelectedDataAsync(imgHtml, {
        coercionType: "image"
    },
    function (asyncResult) {
        if (asyncResult.status == "failed") {
            writeError('Error: ' + asyncResult.error.message);
        }
    });

Word API:

The Word API includes a insertInlinePictureFromBase64 method. As with the setSelectedDataAsync, it takes an image in as a Base 64 encoded string:

Word.run(function (context) {
    context.document.body.insertFileFromBase64(base64String, "end");
    return context.sync();
}).catch(function (myError) {
    // Handle error
})

相关问答

更多
  • 实际上,你使用的版本可能并不重要(除非它是2003/4或之前)。 通常,这样做的方法是创建构建块(或快速部分,这是他们在Word 2010中调用的)。 此链接说明了您将采取的基本步骤(尽管如果您不使用Word 2010/13,详细信息会有所不同): https : //support.office.com/en-SG/Article/Word-2010-Create-一个全新的模板- cb17846d-ECEC-49d4-82ea-a6f5e3e8b9ae #__ toc278287931 基本上,您将图像 ...
  • 说实话,我仍然不知道你的问题究竟在哪里。 我假设您拥有使用VBA从数据库中获取bookmark name和url知识和代码。 如果是这样,将会有非常简单的代码,允许您将图片从web加载到word文档中的书签。 下面是我测试过的代码的一半成功。 如果我添加任何图片,它将工作正常。 但不会与活跃的谷歌地图的网址一起使用。 我不知道你的意思是什么'静态谷歌地图'(在评论中),你没有提供任何例子,因此你需要自己进行测试。 在运行此测试之前,请确保您的活动文档中有两个书签: bookmark_logo和bookmar ...
  • 一种解决方案是使用书签:请参阅https://social.msdn.microsoft.com/Forums/vstudio/en-US/f4f6ecdd-ede9-4f14-83c9-bcad018a444b/insert-image-at-bookmark-position-在字的文档- progamatically-C?论坛= csharpgeneral 而且,如果图像没有显示在Microsoft Word上,这里有一个解决方案: http : //wordfaqs.mvps.org/Invisib ...
  • 用于添加到Word文档的图像 ActiveDocument.Shapes.AddPicture Anchor:= Selection.Range, fileName:=localFileName, LinkToFile:=True, SaveWithDocument:=True 方法,该链接可用 ShapeOfInterest.LinkFormat.SourceFullName 这仅在文档中存在链接时才有效。 您可以通过在信息屏幕上查看文档,在“相关文档”下面出现“编辑文件链接”菜单项来了解情况。 对于 ...
  • 如果它只是你要在word文档中添加的图像,那么使用它, Sub FillABookmark(bookmarkname As String, imagepath As String) Dim objWord As Object Dim objDoc As Object On Error Resume Next Set objWord = GetObject(, "Word.Application") If objWord Is Nothing Then ...
  • 根据MDN页面 : keep-all值在webkit中未实现(参见WebKit bug 43917 )。 关于SO的类似主题(查看@MrAlien的回答): Safari CSS word-break:keep-all; 不管用 According to MDN page: keep-all value is unimplemented in webkit (see WebKit bug 43917). Similar topic on SO (Check @MrAlien's answer): Safar ...
  • 这里混合了两种不同的API约定。 使用Shared API时不使用run上下文,它仅在调用Word API时使用 。 一般来说,我会推荐使用Word API,因为它更新,整体上提供了更多的功能。 也就是说,对于此操作,您可以使用Word或Shared API。 我将提供两个例子。 还要注意.toDataURL()返回一个包含 base64图像的字符串。 它不是纯粹的base64表示。 您需要删除Word之前的data:image/png;base64,前缀才能够呈现它。 共享API: 既然你只是插入一个图像 ...
  • 我已经解决了我的问题。 我在这里发布我的答案,也许这对其他人有帮助。 var doc = new Document(); var shapeCollection = doc.GetChildNodes(Word.NodeType.Shape, true); // getting all images from Word document. foreach (Shape shape in shapeCollection) ...
  • 尝试这个 With ActiveDocument.InlineShapes(ActiveDocument.InlineShapes.Count) .Height = 314.95 ' or whatever End With 这假定图片是“与文本一致”(正如您所做的那样)并且是文档中最后一个(最下面的)内嵌图片。 Try this With ActiveDocument.InlineShapes(ActiveDocument.InlineShapes.Count) .Height = 31 ...
  • 使用第三方组件:Aspose.Words,它可以为您完成。 Document doc = new Document(MyDir + "Document.doc"); doc.Save(MyDir + "Document.ConvertToHtml Out.html", SaveFormat.Html); 它还支持保存到Png格式。 SaveFormat.Png:呈现文档的页面并将其另存为PNG文件。 use a third party component: Aspose.Words, it can do ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。